# Generated by Django 4.2.7 on 2025-12-15 16:36

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


class Migration(migrations.Migration):
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ("schedule", "0004_lessonhomeworksubmission_and_more"),
    ]

    operations = [
        migrations.AddField(
            model_name="lesson",
            name="subject_name",
            field=models.CharField(
                blank=True,
                help_text="Название предмета для обратной совместимости",
                max_length=100,
                verbose_name="Название предмета (legacy)",
            ),
        ),
        migrations.AddField(
            model_name="lessontemplate",
            name="subject_name",
            field=models.CharField(
                blank=True, max_length=100, verbose_name="Название предмета (legacy)"
            ),
        ),
        migrations.CreateModel(
            name="Subject",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="Название предмета обучения",
                        max_length=100,
                        unique=True,
                        verbose_name="Название предмета",
                    ),
                ),
                (
                    "is_active",
                    models.BooleanField(
                        default=True,
                        help_text="Доступен ли предмет для использования",
                        verbose_name="Активен",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Дата создания"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(auto_now=True, verbose_name="Дата обновления"),
                ),
            ],
            options={
                "verbose_name": "Предмет",
                "verbose_name_plural": "Предметы",
                "db_table": "subjects",
                "ordering": ["name"],
                "indexes": [
                    models.Index(
                        fields=["is_active", "name"], name="subjects_is_acti_dc7d22_idx"
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="MentorSubject",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "name",
                    models.CharField(
                        help_text="Название кастомного предмета",
                        max_length=100,
                        verbose_name="Название предмета",
                    ),
                ),
                (
                    "usage_count",
                    models.IntegerField(
                        default=0,
                        help_text="Сколько раз этот предмет был использован в занятиях",
                        verbose_name="Количество использований",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(
                        auto_now_add=True, verbose_name="Дата создания"
                    ),
                ),
                (
                    "updated_at",
                    models.DateTimeField(auto_now=True, verbose_name="Дата обновления"),
                ),
                (
                    "mentor",
                    models.ForeignKey(
                        limit_choices_to={"role": "mentor"},
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="mentor_subjects",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="Ментор",
                    ),
                ),
            ],
            options={
                "verbose_name": "Предмет ментора",
                "verbose_name_plural": "Предметы менторов",
                "db_table": "mentor_subjects",
                "ordering": ["mentor", "name"],
            },
        ),
        migrations.AddField(
            model_name="lesson",
            name="mentor_subject",
            field=models.ForeignKey(
                blank=True,
                help_text="Кастомный предмет ментора (если не выбран общий предмет)",
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="lessons",
                to="schedule.mentorsubject",
                verbose_name="Кастомный предмет",
            ),
        ),
        migrations.AddField(
            model_name="lessontemplate",
            name="mentor_subject",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                related_name="templates",
                to="schedule.mentorsubject",
                verbose_name="Кастомный предмет",
            ),
        ),
        # Временно оставляем subject как CharField, изменим в следующей миграции
        # migrations.AlterField(
        #     model_name="lesson",
        #     name="subject",
        #     field=models.ForeignKey(
        #         blank=True,
        #         help_text="Общий предмет обучения",
        #         null=True,
        #         on_delete=django.db.models.deletion.SET_NULL,
        #         related_name="lessons",
        #         to="schedule.subject",
        #         verbose_name="Предмет",
        #     ),
        # ),
        # migrations.AlterField(
        #     model_name="lessontemplate",
        #     name="subject",
        #     field=models.ForeignKey(
        #         blank=True,
        #         null=True,
        #         on_delete=django.db.models.deletion.SET_NULL,
        #         related_name="templates",
        #         to="schedule.subject",
        #         verbose_name="Предмет",
        #     ),
        # ),
        migrations.AddIndex(
            model_name="mentorsubject",
            index=models.Index(
                fields=["mentor", "name"], name="mentor_subj_mentor__bed40f_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="mentorsubject",
            index=models.Index(
                fields=["name", "usage_count"], name="mentor_subj_name_79d249_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="mentorsubject",
            constraint=models.UniqueConstraint(
                fields=("mentor", "name"), name="unique_mentor_subject"
            ),
        ),
    ]
