# Generated by Django 4.2.16 on 2026-10-29 01:12

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


def keep_old_coupons_unlimited(apps, schema_editor):
    # Coupons made before this update had no per-customer limit; keep it that way.
    apps.get_model('store', 'Coupon').objects.update(per_customer_limit=None)


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('store', '0008_bangla_seo_defaults'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='coupon',
            options={'ordering': ['-created_at']},
        ),
        migrations.AddField(
            model_name='coupon',
            name='applies_to',
            field=models.CharField(choices=[('all', 'Everything (shop + digital magazines)'), ('magazines', 'Magazines — printed and digital'), ('magazine_digital', 'Digital magazines only'), ('magazine_print', 'Printed magazines only'), ('merch', 'Wearable history (merch) only'), ('nostalgic', 'Nostalgic items only'), ('selected', 'Only the products picked below')], default='all', max_length=20),
        ),
        migrations.AddField(
            model_name='coupon',
            name='max_discount',
            field=models.DecimalField(blank=True, decimal_places=2, help_text='Cap for percentage coupons, e.g. 20% but never more than ৳200. Empty = no cap.', max_digits=10, null=True, verbose_name='Maximum discount (৳)'),
        ),
        migrations.AddField(
            model_name='coupon',
            name='min_order_amount',
            field=models.DecimalField(blank=True, decimal_places=2, help_text='Coupon works only when the eligible items cost at least this much.', max_digits=10, null=True, verbose_name='Minimum order (৳)'),
        ),
        migrations.AddField(
            model_name='coupon',
            name='note',
            field=models.CharField(blank=True, help_text="Internal note, e.g. 'Facebook winter campaign'.", max_length=200),
        ),
        migrations.AddField(
            model_name='coupon',
            name='owner',
            field=models.ForeignKey(blank=True, help_text='If set, only this account can use the code.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='coupons', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddField(
            model_name='coupon',
            name='per_customer_limit',
            field=models.PositiveIntegerField(blank=True, default=1, help_text='Counted by account and by phone number. Empty = unlimited.', null=True, verbose_name='Uses per customer'),
        ),
        migrations.AddField(
            model_name='coupon',
            name='products',
            field=models.ManyToManyField(blank=True, help_text="Used when 'Applies to' is 'Only the products picked below'.", related_name='coupons', to='store.product'),
        ),
        migrations.AddField(
            model_name='coupon',
            name='requires_login',
            field=models.BooleanField(default=False, verbose_name='Signed-in customers only'),
        ),
        migrations.AddField(
            model_name='coupon',
            name='source',
            field=models.CharField(choices=[('manual', 'Made in admin'), ('game', 'Game reward')], default='manual', editable=False, max_length=10),
        ),
        migrations.AddField(
            model_name='digitalaccess',
            name='coupon',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='digital_orders', to='store.coupon'),
        ),
        migrations.AddField(
            model_name='digitalaccess',
            name='discount_amount',
            field=models.DecimalField(decimal_places=2, default=0, max_digits=8),
        ),
        migrations.AlterField(
            model_name='coupon',
            name='code',
            field=models.CharField(help_text='Coupon code (e.g. WINTER20). Not case-sensitive.', max_length=50, unique=True),
        ),
        migrations.AlterField(
            model_name='coupon',
            name='max_uses',
            field=models.PositiveIntegerField(blank=True, default=100, help_text='How many times the code can be used in total. Empty = unlimited.', null=True, verbose_name='Total uses allowed'),
        ),
        migrations.AlterField(
            model_name='digitalaccess',
            name='amount',
            field=models.DecimalField(decimal_places=2, help_text='What the reader should have sent (after any coupon)', max_digits=8),
        ),
        migrations.CreateModel(
            name='CouponRedemption',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('phone', models.CharField(blank=True, db_index=True, max_length=20)),
                ('amount', models.DecimalField(decimal_places=2, max_digits=10, verbose_name='Discount given (৳)')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('coupon', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='redemptions', to='store.coupon')),
                ('digital_access', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='redemptions', to='store.digitalaccess')),
                ('order', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='redemptions', to='store.order')),
                ('user', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='coupon_redemptions', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),
        migrations.RunPython(keep_old_coupons_unlimited, migrations.RunPython.noop),
    ]
