# Generated by Django 3.2.18 on 2023-05-09 23:55

import django.core.validators
import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("cap_discount", "0002_version_2_0_0"),
    ]

    def forwards_migrate_cap_discount(apps, schema_editor):
        CAPDiscountConfiguration = apps.get_model("cap_discount", "CAPDiscountConfiguration")
        CAPDiscount = apps.get_model("cap_discount", "CAPDiscount")
        CAPDiscountTier = apps.get_model("cap_discount", "CAPDiscountTier")
        for config in CAPDiscountConfiguration.objects.all():
            config.capdiscounttier_set.set(
                [
                    CAPDiscountTier.objects.create(
                        cap_discount_configuration=config, amount=config.amount, discount=config.discount
                    )
                ]
            )
        for discount in CAPDiscount.objects.all():
            discount.capdiscounttier_set.set(
                [
                    CAPDiscountTier.objects.create(
                        cap_discount=discount, amount=discount.amount, discount=discount.discount
                    )
                ]
            )

    def reverse_migrate_cap_discount(apps, schema_editor):
        CAPDiscountConfiguration = apps.get_model("cap_discount", "CAPDiscountConfiguration")
        CAPDiscount = apps.get_model("cap_discount", "CAPDiscount")
        for config in CAPDiscountConfiguration.objects.all():
            first_tier = config.capdiscounttier_set.first()
            config.amount = first_tier.amount
            config.discount = first_tier.discount
            config.save(update_fields=["amount", "discount"])
        for discount in CAPDiscount.objects.all():
            first_tier = discount.capdiscounttier_set.first()
            discount.amount = first_tier.amount
            discount.discount = first_tier.discount
            discount.save(update_fields=["amount", "discount"])

    operations = [
        migrations.CreateModel(
            name="CAPDiscountTier",
            fields=[
                ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "amount",
                    models.DecimalField(
                        decimal_places=2,
                        help_text="Amount after which the discount will be applied",
                        max_digits=14,
                        validators=[django.core.validators.MinValueValidator(1)],
                    ),
                ),
                (
                    "discount",
                    models.DecimalField(
                        decimal_places=3,
                        help_text="Discount in percent. Ex 20.5%",
                        max_digits=6,
                        validators=[
                            django.core.validators.MinValueValidator(0),
                            django.core.validators.MaxValueValidator(100),
                        ],
                    ),
                ),
                (
                    "cap_discount",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="cap_discount.capdiscount",
                    ),
                ),
                (
                    "cap_discount_configuration",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="cap_discount.capdiscountconfiguration",
                    ),
                ),
            ],
            options={
                "ordering": ["amount"],
            },
        ),
        migrations.RunPython(forwards_migrate_cap_discount, reverse_migrate_cap_discount),
        migrations.AlterField(
            model_name="capdiscount",
            name="amount",
            field=models.DecimalField(
                default=0,
                decimal_places=2,
                help_text="Amount after which the discount will be applied",
                max_digits=14,
                validators=[django.core.validators.MinValueValidator(1)],
            ),
            preserve_default=True,
        ),
        migrations.RemoveField(
            model_name="capdiscount",
            name="amount",
        ),
        migrations.AlterField(
            model_name="capdiscount",
            name="discount",
            field=models.DecimalField(
                default=0,
                decimal_places=3,
                help_text="Discount in percent. Ex 20.5%",
                max_digits=6,
                validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(100)],
            ),
        ),
        migrations.RemoveField(
            model_name="capdiscount",
            name="discount",
        ),
        migrations.AlterField(
            model_name="capdiscountconfiguration",
            name="amount",
            field=models.DecimalField(
                default=0,
                decimal_places=2,
                help_text="Amount after which the discount will be applied",
                max_digits=14,
                validators=[django.core.validators.MinValueValidator(1)],
            ),
        ),
        migrations.RemoveField(
            model_name="capdiscountconfiguration",
            name="amount",
        ),
        migrations.AlterField(
            model_name="capdiscountconfiguration",
            name="discount",
            field=models.DecimalField(
                default=0,
                decimal_places=3,
                help_text="Discount in percent. Ex 20.5%",
                max_digits=6,
                validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(100)],
            ),
            preserve_default=True,
        ),
        migrations.RemoveField(
            model_name="capdiscountconfiguration",
            name="discount",
        ),
    ]
