# Generated by Django 5.2.5 on 2025-08-11 17:57

from django.db import migrations, models
import django.db.models.deletion
from django.utils import timezone

def add_default_timestamps(apps, schema_editor):
    """Add default timestamps to existing Cart and CartItem records"""
    Cart = apps.get_model('shop', 'Cart')
    CartItem = apps.get_model('shop', 'CartItem')
    
    # Update existing Cart records
    for cart in Cart.objects.all():
        cart.created_at = timezone.now()
        cart.updated_at = timezone.now()
        cart.save()
    
    # Update existing CartItem records
    for item in CartItem.objects.all():
        item.added_at = timezone.now()
        item.save()

def reverse_default_timestamps(apps, schema_editor):
    """Reverse function - no need to do anything"""
    pass

class Migration(migrations.Migration):

    dependencies = [
        ('shop', '0005_userprofile'),
    ]

    operations = [
        # Add default timestamps to existing records
        migrations.RunPython(add_default_timestamps, reverse_default_timestamps),
        
        # Add new fields to existing models
        migrations.AddField(
            model_name='cart',
            name='created_at',
            field=models.DateTimeField(auto_now_add=True, default=timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='cart',
            name='updated_at',
            field=models.DateTimeField(auto_now=True, default=timezone.now),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='cartitem',
            name='added_at',
            field=models.DateTimeField(auto_now_add=True, default=timezone.now),
            preserve_default=False,
        ),
        
        # Add coupon models
        migrations.CreateModel(
            name='Coupon',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('code', models.CharField(help_text='Coupon code (e.g., SAVE20, WELCOME10)', max_length=20, unique=True)),
                ('name', models.CharField(help_text='Display name for the coupon', max_length=100)),
                ('description', models.TextField(blank=True, help_text='Description of the coupon offer')),
                ('discount_type', models.CharField(choices=[('percentage', 'Percentage'), ('fixed', 'Fixed Amount')], default='percentage', max_length=10)),
                ('discount_value', models.DecimalField(decimal_places=2, help_text='Discount amount or percentage', max_digits=10)),
                ('minimum_order_amount', models.DecimalField(decimal_places=2, default=0, help_text='Minimum order amount to apply coupon', max_digits=10)),
                ('maximum_discount', models.DecimalField(blank=True, decimal_places=2, help_text='Maximum discount amount (for percentage coupons)', max_digits=10, null=True)),
                ('usage_limit', models.PositiveIntegerField(default=1, help_text='Maximum number of times this coupon can be used')),
                ('usage_limit_per_user', models.PositiveIntegerField(default=1, help_text='Maximum number of times a single user can use this coupon')),
                ('valid_from', models.DateTimeField(help_text='Start date and time when coupon becomes valid')),
                ('valid_until', models.DateTimeField(help_text='End date and time when coupon expires')),
                ('is_active', models.BooleanField(default=True, help_text='Whether the coupon is currently active')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.CreateModel(
            name='CouponUsage',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('order_amount', models.DecimalField(decimal_places=2, help_text='Order amount before discount', max_digits=10)),
                ('discount_amount', models.DecimalField(decimal_places=2, help_text='Discount amount applied', max_digits=10)),
                ('used_at', models.DateTimeField(auto_now_add=True)),
                ('coupon', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='coupon_usage', to='shop.coupon')),
                ('order', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='coupon_usage', to='shop.order')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='coupon_usage', to='auth.user')),
            ],
            options={
                'ordering': ['-used_at'],
                'unique_together': {('coupon', 'order')},
            },
        ),
        
        # Update Order model
        migrations.AddField(
            model_name='order',
            name='applied_coupon',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='orders', to='shop.coupon'),
        ),
        migrations.AddField(
            model_name='order',
            name='discount_amount',
            field=models.DecimalField(decimal_places=2, default=0, max_digits=10),
        ),
        migrations.AddField(
            model_name='order',
            name='final_amount',
            field=models.DecimalField(decimal_places=2, max_digits=10, null=True),
        ),
        migrations.AddField(
            model_name='order',
            name='status',
            field=models.CharField(choices=[('pending', 'Pending'), ('processing', 'Processing'), ('shipped', 'Shipped'), ('delivered', 'Delivered'), ('cancelled', 'Cancelled')], default='pending', max_length=20),
        ),
        migrations.AddField(
            model_name='order',
            name='updated_at',
            field=models.DateTimeField(auto_now=True, default=timezone.now),
            preserve_default=False,
        ),
        migrations.RenameField(
            model_name='order',
            old_name='total',
            new_name='total_amount',
        ),
    ]
