# Part 4 — question bank + attempt_questions (NOT APPLIED YET)
# Review, then: python manage.py migrate courses 0008

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('courses', '0007_course_pricing_and_subscriptions'),
    ]

    operations = [
        migrations.CreateModel(
            name='CourseQuestion',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('question_text', models.TextField()),
                ('option_a', models.CharField(max_length=500)),
                ('option_b', models.CharField(max_length=500)),
                ('option_c', models.CharField(max_length=500)),
                ('option_d', models.CharField(max_length=500)),
                ('correct_option', models.CharField(choices=[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')], max_length=1)),
                ('difficulty', models.CharField(choices=[('easy', 'Easy'), ('medium', 'Medium'), ('hard', 'Hard')], db_index=True, default='medium', help_text='Target mix ~40% easy / 40% medium / 20% hard (not DB-enforced).', max_length=10)),
                ('topic_tag', models.CharField(blank=True, db_index=True, help_text='Subtopic slug, e.g. on-page, technical-seo — used for balanced draws.', max_length=80)),
                ('is_active', models.BooleanField(db_index=True, default=True)),
                ('is_verified', models.BooleanField(db_index=True, default=False, help_text='SME must verify before question can appear in a live exam.')),
                ('last_reviewed_at', models.DateTimeField(blank=True, null=True)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='bank_questions', to='courses.course')),
                ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='created_bank_questions', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'Course question',
                'verbose_name_plural': 'Course questions',
                'ordering': ['course', 'topic_tag', 'id'],
            },
        ),
        migrations.CreateModel(
            name='CourseExamAttempt',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('status', models.CharField(choices=[('started', 'Started'), ('submitted', 'Submitted'), ('expired', 'Expired'), ('cancelled', 'Cancelled')], db_index=True, default='started', max_length=20)),
                ('started_at', models.DateTimeField(auto_now_add=True)),
                ('submitted_at', models.DateTimeField(blank=True, null=True)),
                ('score_percent', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)),
                ('course', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='exam_attempts', to='courses.course')),
                ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='course_exam_attempts', to=settings.AUTH_USER_MODEL)),
            ],
            options={
                'verbose_name': 'Course exam attempt',
                'verbose_name_plural': 'Course exam attempts',
                'ordering': ['-started_at'],
            },
        ),
        migrations.CreateModel(
            name='AttemptQuestion',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('order_index', models.PositiveIntegerField()),
                ('option_order', models.JSONField(help_text="Original options in display order, e.g. ['B','A','D','C'].")),
                ('presented_question_text', models.TextField()),
                ('presented_option_a', models.CharField(max_length=500)),
                ('presented_option_b', models.CharField(max_length=500)),
                ('presented_option_c', models.CharField(max_length=500)),
                ('presented_option_d', models.CharField(max_length=500)),
                ('correct_display_option', models.CharField(choices=[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')], help_text='Which displayed letter (A–D) is correct after shuffle.', max_length=1)),
                ('selected_display_option', models.CharField(blank=True, choices=[('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D')], max_length=1)),
                ('attempt', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='attempt_questions', to='courses.courseexamattempt')),
                ('question', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='attempt_appearances', to='courses.coursequestion')),
            ],
            options={
                'verbose_name': 'Attempt question',
                'verbose_name_plural': 'Attempt questions',
                'ordering': ['attempt', 'order_index'],
            },
        ),
        migrations.AddIndex(
            model_name='coursequestion',
            index=models.Index(fields=['course', 'is_active', 'is_verified'], name='cq_eligible_idx'),
        ),
        migrations.AddConstraint(
            model_name='attemptquestion',
            constraint=models.UniqueConstraint(fields=('attempt', 'question'), name='uniq_attempt_bank_question'),
        ),
        migrations.AddConstraint(
            model_name='attemptquestion',
            constraint=models.UniqueConstraint(fields=('attempt', 'order_index'), name='uniq_attempt_question_order'),
        ),
    ]
