# Generated by Django 4.2.7 on 2025-12-19 08:13

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


class Migration(migrations.Migration):
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("subscriptions", "0001_initial"),
    ]

    operations = [
        migrations.AddField(
            model_name="subscription",
            name="discount_amount",
            field=models.DecimalField(
                decimal_places=2,
                default=0,
                max_digits=10,
                validators=[django.core.validators.MinValueValidator(0)],
                verbose_name="Сумма скидки",
            ),
        ),
        migrations.AddField(
            model_name="subscription",
            name="duration_days",
            field=models.IntegerField(
                default=30,
                help_text="30, 90, 180, 365 дней",
                validators=[django.core.validators.MinValueValidator(1)],
                verbose_name="Длительность в днях",
            ),
        ),
        migrations.AddField(
            model_name="subscription",
            name="final_amount",
            field=models.DecimalField(
                decimal_places=2,
                default=0,
                max_digits=10,
                validators=[django.core.validators.MinValueValidator(0)],
                verbose_name="Итоговая сумма",
            ),
        ),
        migrations.AddField(
            model_name="subscription",
            name="original_amount",
            field=models.DecimalField(
                decimal_places=2,
                default=0,
                max_digits=10,
                validators=[django.core.validators.MinValueValidator(0)],
                verbose_name="Исходная сумма",
            ),
        ),
        migrations.AddField(
            model_name="subscription",
            name="student_count",
            field=models.IntegerField(
                default=0,
                help_text='Используется для типа подписки "За ученика"',
                validators=[django.core.validators.MinValueValidator(0)],
                verbose_name="Количество учеников",
            ),
        ),
        migrations.AddField(
            model_name="subscriptionplan",
            name="price_per_student",
            field=models.DecimalField(
                blank=True,
                decimal_places=2,
                help_text='Используется для типа "За ученика"',
                max_digits=10,
                null=True,
                validators=[django.core.validators.MinValueValidator(0)],
                verbose_name="Цена за ученика",
            ),
        ),
        migrations.AddField(
            model_name="subscriptionplan",
            name="subscription_type",
            field=models.CharField(
                choices=[("per_student", "За ученика"), ("monthly", "Ежемесячная")],
                default="monthly",
                max_length=20,
                verbose_name="Тип подписки",
            ),
        ),
        migrations.CreateModel(
            name="PromoCode",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "code",
                    models.CharField(
                        db_index=True,
                        help_text="Уникальный код промокода",
                        max_length=50,
                        unique=True,
                        verbose_name="Код",
                    ),
                ),
                ("description", models.TextField(blank=True, verbose_name="Описание")),
                (
                    "discount_type",
                    models.CharField(
                        choices=[
                            ("percentage", "Процент"),
                            ("fixed", "Фиксированная сумма"),
                        ],
                        default="percentage",
                        max_length=20,
                        verbose_name="Тип скидки",
                    ),
                ),
                (
                    "discount_value",
                    models.DecimalField(
                        decimal_places=2,
                        help_text="Процент или фиксированная сумма",
                        max_digits=10,
                        validators=[django.core.validators.MinValueValidator(0)],
                        verbose_name="Значение скидки",
                    ),
                ),
                (
                    "max_discount_amount",
                    models.DecimalField(
                        blank=True,
                        decimal_places=2,
                        help_text="Для процентных скидок - ограничение максимальной суммы",
                        max_digits=10,
                        null=True,
                        validators=[django.core.validators.MinValueValidator(0)],
                        verbose_name="Максимальная сумма скидки",
                    ),
                ),
                (
                    "valid_from",
                    models.DateTimeField(
                        blank=True,
                        help_text="Если не указано, действует с момента создания",
                        null=True,
                        verbose_name="Действителен с",
                    ),
                ),
                (
                    "valid_until",
                    models.DateTimeField(
                        blank=True,
                        help_text="Если не указано, действует бессрочно",
                        null=True,
                        verbose_name="Действителен до",
                    ),
                ),
                (
                    "max_uses_total",
                    models.IntegerField(
                        blank=True,
                        help_text="Общее количество использований промокода",
                        null=True,
                        validators=[django.core.validators.MinValueValidator(1)],
                        verbose_name="Максимум использований (общий)",
                    ),
                ),
                (
                    "max_uses_per_user",
                    models.IntegerField(
                        default=1,
                        help_text="Сколько раз один пользователь может использовать промокод",
                        validators=[django.core.validators.MinValueValidator(1)],
                        verbose_name="Максимум использований на пользователя",
                    ),
                ),
                (
                    "uses_count",
                    models.IntegerField(
                        default=0, verbose_name="Количество использований"
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        db_index=True, default=True, verbose_name="Активен"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Дата создания"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(auto_now=True, verbose_name="Дата обновления"),
                ),
                (
                    "applicable_plans",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Если не выбрано, применяется ко всем тарифам",
                        related_name="promo_codes",
                        to="subscriptions.subscriptionplan",
                        verbose_name="Применимые тарифы",
                    ),
                ),
            ],
            options={
                "verbose_name": "Промокод",
                "verbose_name_plural": "Промокоды",
                "db_table": "promo_codes",
                "ordering": ["-created_at"],
            },
        ),
        migrations.AddField(
            model_name="subscription",
            name="promo_code",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="subscriptions",
                to="subscriptions.promocode",
                verbose_name="Промокод",
            ),
        ),
        migrations.CreateModel(
            name="PromoCodeUsage",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "discount_amount",
                    models.DecimalField(
                        decimal_places=2, max_digits=10, verbose_name="Сумма скидки"
                    ),
                ),
                (
                    "original_amount",
                    models.DecimalField(
                        decimal_places=2, max_digits=10, verbose_name="Исходная сумма"
                    ),
                ),
                (
                    "final_amount",
                    models.DecimalField(
                        decimal_places=2, max_digits=10, verbose_name="Итоговая сумма"
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True,
                        db_index=True,
                        verbose_name="Дата использования",
                    ),
                ),
                (
                    "promo_code",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="usages",
                        to="subscriptions.promocode",
                        verbose_name="Промокод",
                    ),
                ),
                (
                    "subscription",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="promo_code_usages",
                        to="subscriptions.subscription",
                        verbose_name="Подписка",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="promo_code_usages",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Пользователь",
                    ),
                ),
            ],
            options={
                "verbose_name": "Использование промокода",
                "verbose_name_plural": "Использования промокодов",
                "db_table": "promo_code_usages",
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["user", "created_at"],
                        name="promo_code__user_id_880451_idx",
                    ),
                    models.Index(
                        fields=["promo_code", "user"],
                        name="promo_code__promo_c_69f07c_idx",
                    ),
                ],
            },
        ),
        migrations.AddIndex(
            model_name="promocode",
            index=models.Index(
                fields=["code", "is_active"], name="promo_codes_code_ece6cf_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="promocode",
            index=models.Index(
                fields=["valid_until"], name="promo_codes_valid_u_7b2da6_idx"
            ),
        ),
    ]
