# Generated by Django 5.2.11 on 2026-02-18 13:30

import django.db.models.deletion
from django.db import migrations, models
from django.db.migrations.state import StateApps

import counter.fields


def migrate_prices(apps: StateApps, schema_editor):
    Product = apps.get_model("counter", "Product")
    Price = apps.get_model("counter", "Price")
    prices = [
        Price(
            amount=p.selling_price,
            product=p,
            created_at=p.created_at,
            updated_at=p.updated_at,
        )
        for p in Product.objects.all()
    ]
    Price.objects.bulk_create(prices)
    groups = [
        Price.groups.through(price=price, group=group)
        for price in Price.objects.select_related("product").prefetch_related(
            "product__buying_groups"
        )
        for group in price.product.buying_groups.all()
    ]
    Price.groups.through.objects.bulk_create(groups)


class Migration(migrations.Migration):
    dependencies = [
        ("core", "0048_alter_user_options"),
        ("counter", "0038_countersellers"),
    ]

    operations = [
        migrations.CreateModel(
            name="Price",
            fields=[
                (
                    "id",
                    models.AutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "amount",
                    counter.fields.CurrencyField(
                        decimal_places=2, max_digits=12, verbose_name="amount"
                    ),
                ),
                (
                    "is_always_shown",
                    models.BooleanField(
                        default=False,
                        help_text=(
                            "If this option is enabled, "
                            "people will see this price and be able to pay it, "
                            "even if another cheaper price exists. "
                            "Else it will visible only if it is the cheapest available price."
                        ),
                        verbose_name="always show",
                    ),
                ),
                (
                    "label",
                    models.CharField(
                        default="",
                        help_text=(
                            "A short label for easier differentiation "
                            "if a user can see multiple prices."
                        ),
                        max_length=32,
                        verbose_name="label",
                        blank=True,
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now_add=True, verbose_name="created at"),
                ),
                (
                    "updated_at",
                    models.DateTimeField(auto_now=True, verbose_name="updated at"),
                ),
                (
                    "groups",
                    models.ManyToManyField(
                        related_name="prices", to="core.group", verbose_name="groups"
                    ),
                ),
                (
                    "product",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="prices",
                        to="counter.product",
                        verbose_name="product",
                    ),
                ),
            ],
            options={"verbose_name": "price"},
        ),
        migrations.AlterField(
            model_name="product",
            name="tray",
            field=models.BooleanField(
                default=False,
                help_text="Buy five, get the sixth free",
                verbose_name="tray price",
            ),
        ),
        migrations.RunPython(migrate_prices, reverse_code=migrations.RunPython.noop),
        migrations.RemoveField(model_name="product", name="selling_price"),
        migrations.RemoveField(model_name="product", name="special_selling_price"),
        migrations.AlterField(
            model_name="product",
            name="description",
            field=models.TextField(blank=True, default="", verbose_name="description"),
        ),
        migrations.AlterField(
            model_name="product",
            name="product_type",
            field=models.ForeignKey(
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="products",
                to="counter.producttype",
                verbose_name="product type",
            ),
        ),
        migrations.AlterField(
            model_name="productformula",
            name="result",
            field=models.OneToOneField(
                help_text="The product got with the formula.",
                on_delete=django.db.models.deletion.CASCADE,
                related_name="formula",
                to="counter.product",
                verbose_name="result product",
            ),
        ),
    ]
