# Part 5 — exam engine fields + attempt_events (NOT APPLIED YET)
# Review, then: python manage.py migrate courses 0009

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('courses', '0008_question_bank'),
    ]

    operations = [
        migrations.AddField(
            model_name='courseexamattempt',
            name='timer_started_at',
            field=models.DateTimeField(blank=True, help_text='Set when the candidate finishes practice and the timed exam begins.', null=True),
        ),
        migrations.AddField(
            model_name='courseexamattempt',
            name='correct_count',
            field=models.PositiveIntegerField(default=0),
        ),
        migrations.AddField(
            model_name='courseexamattempt',
            name='total_asked',
            field=models.PositiveIntegerField(default=0),
        ),
        migrations.AddField(
            model_name='courseexamattempt',
            name='passed',
            field=models.BooleanField(default=False),
        ),
        migrations.AddField(
            model_name='courseexamattempt',
            name='time_taken_seconds',
            field=models.PositiveIntegerField(blank=True, help_text='Seconds from timer start to submit (null if never timed).', null=True),
        ),
        migrations.AddField(
            model_name='courseexamattempt',
            name='auto_submitted',
            field=models.BooleanField(default=False, help_text='True when the server/client submitted because the timer expired.'),
        ),
        migrations.AddField(
            model_name='courseexamattempt',
            name='is_flagged_for_review',
            field=models.BooleanField(db_index=True, default=False, help_text='Soft flag for staff review (e.g. ≥3 tab switches). Does not auto-fail.'),
        ),
        migrations.AddField(
            model_name='courseexamattempt',
            name='flag_reason',
            field=models.CharField(blank=True, max_length=255),
        ),
        migrations.AddField(
            model_name='courseexamattempt',
            name='tab_switch_count',
            field=models.PositiveIntegerField(default=0, help_text='Counted tab/window leave events during the timed exam.'),
        ),
        migrations.AddField(
            model_name='courseexamattempt',
            name='topic_breakdown',
            field=models.JSONField(blank=True, default=list, help_text='Per-topic results, e.g. [{"topic_tag","label","correct","total","percent"}, ...].'),
        ),
        migrations.AddField(
            model_name='attemptquestion',
            name='is_correct',
            field=models.BooleanField(blank=True, help_text='Set on submit; null while the attempt is in progress.', null=True),
        ),
        migrations.CreateModel(
            name='AttemptEvent',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('event_type', models.CharField(choices=[('tab_switch', 'Tab / visibility switch'), ('window_blur', 'Window blur'), ('fullscreen_exit', 'Exited full screen'), ('fullscreen_enter', 'Entered full screen'), ('copy_attempt', 'Copy attempt'), ('paste_attempt', 'Paste attempt'), ('nav_back', 'Browser back/forward'), ('warning_shown', 'On-screen warning shown'), ('auto_submit_expiry', 'Auto-submit on timer expiry'), ('manual_submit', 'Manual submit'), ('webcam_snapshot_stub', 'Webcam snapshot (stub — not implemented)')], db_index=True, max_length=40)),
                ('detail', models.CharField(blank=True, max_length=255)),
                ('metadata', models.JSONField(blank=True, default=dict)),
                ('created_at', models.DateTimeField(auto_now_add=True, db_index=True)),
                ('attempt', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='events', to='courses.courseexamattempt')),
            ],
            options={
                'verbose_name': 'Attempt event',
                'verbose_name_plural': 'Attempt events',
                'ordering': ['attempt', 'created_at'],
            },
        ),
    ]
