# academics/migrations/0004_grades_and_reportcards.py
"""
Migration for AcademicTerm, ReportSubject, MonthlyMark,
TermSynthesis, and ReportCard models.

Run:
    python manage.py migrate academics
"""

from django.db import migrations, models
import django.core.validators
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

    dependencies = [
        ('academics', '0003_alter_attendance_date_alter_attendance_synced_at_and_more'),
        ('students',  '0002_alter_student_date_of_birth'),
        ('staff',     '0003_alter_staff_options_staff_alt_phone_country_code_and_more'),
    ]

    operations = [

        # ── AcademicTerm ─────────────────────────────────────────
        migrations.CreateModel(
            name='AcademicTerm',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('academic_year', models.CharField(default='2025-2026', max_length=9, verbose_name='Academic Year')),
                ('term', models.PositiveSmallIntegerField(choices=[(1,'Term 1'),(2,'Term 2'),(3,'Term 3')], verbose_name='Term')),
                ('start_date', models.DateField(verbose_name='Start Date')),
                ('end_date',   models.DateField(verbose_name='End Date')),
                ('is_current', models.BooleanField(default=False, verbose_name='Current Term')),
            ],
            options={
                'verbose_name': 'Academic Term',
                'verbose_name_plural': 'Academic Terms',
                'ordering': ['academic_year', 'term'],
                'unique_together': {('academic_year', 'term')},
            },
        ),

        # ── ReportSubject ────────────────────────────────────────
        migrations.CreateModel(
            name='ReportSubject',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('report_type',  models.CharField(max_length=2, choices=[('ap','Anglophone Primary'),('an','Anglophone Nursery'),('fp','Francophone Primary'),('fn','Francophone Nursery')])),
                ('domain',       models.CharField(max_length=100, verbose_name='Domain / Activity Group')),
                ('activity',     models.CharField(max_length=100, blank=True, verbose_name='Activity (if nested)')),
                ('sub_activity', models.CharField(max_length=150, verbose_name='Sub-Activity / Subject')),
                ('order',        models.PositiveSmallIntegerField(default=0, verbose_name='Display Order')),
            ],
            options={
                'verbose_name': 'Report Subject',
                'verbose_name_plural': 'Report Subjects',
                'ordering': ['report_type', 'order'],
                'unique_together': {('report_type', 'domain', 'activity', 'sub_activity')},
            },
        ),

        # ── MonthlyMark ──────────────────────────────────────────
        migrations.CreateModel(
            name='MonthlyMark',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('month_number', models.PositiveSmallIntegerField(
                    choices=[(i, f'Month {i}') for i in range(1, 10)],
                    verbose_name='Month Number (1–9)'
                )),
                ('score', models.DecimalField(
                    max_digits=4, decimal_places=1,
                    null=True, blank=True,
                    validators=[
                        django.core.validators.MinValueValidator(0),
                        django.core.validators.MaxValueValidator(20),
                    ],
                    verbose_name='Score (/20)'
                )),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
                ('student', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='monthly_marks',
                    to='students.student'
                )),
                ('term', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='monthly_marks',
                    to='academics.academicterm'
                )),
                ('subject', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='monthly_marks',
                    to='academics.reportsubject'
                )),
                ('entered_by', models.ForeignKey(
                    null=True, blank=True,
                    on_delete=django.db.models.deletion.SET_NULL,
                    related_name='marks_entered',
                    to='staff.staff'
                )),
            ],
            options={
                'verbose_name': 'Monthly Mark',
                'verbose_name_plural': 'Monthly Marks',
                'ordering': ['term', 'month_number', 'subject__order'],
                'unique_together': {('student', 'term', 'subject', 'month_number')},
            },
        ),

        # ── TermSynthesis ────────────────────────────────────────
        migrations.CreateModel(
            name='TermSynthesis',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('synthese_score', models.DecimalField(
                    max_digits=4, decimal_places=2,
                    null=True, blank=True,
                    verbose_name='Synthèse Score (/20)'
                )),
                ('appreciation', models.CharField(
                    max_length=3,
                    choices=[('NA',"NA"), ('ECA',"ECA"), ('A',"A"), ('A+',"A+"), ('',"—")],
                    blank=True,
                    verbose_name='Appréciation'
                )),
                ('remark', models.CharField(max_length=200, blank=True, verbose_name='Remark')),
                ('student', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='term_syntheses',
                    to='students.student'
                )),
                ('term', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='term_syntheses',
                    to='academics.academicterm'
                )),
                ('subject', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='term_syntheses',
                    to='academics.reportsubject'
                )),
            ],
            options={
                'verbose_name': 'Term Synthesis',
                'verbose_name_plural': 'Term Syntheses',
                'unique_together': {('student', 'term', 'subject')},
            },
        ),

        # ── ReportCard ───────────────────────────────────────────
        migrations.CreateModel(
            name='ReportCard',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('report_type', models.CharField(
                    max_length=2,
                    choices=[('ap','Anglophone Primary'),('an','Anglophone Nursery'),
                             ('fp','Francophone Primary'),('fn','Francophone Nursery')],
                    verbose_name='Report Card Type'
                )),
                ('total_marks',   models.DecimalField(max_digits=7, decimal_places=2, default=0)),
                ('average',       models.DecimalField(max_digits=5, decimal_places=2, default=0)),
                ('position',      models.PositiveIntegerField(null=True, blank=True, verbose_name='Position in Class')),
                ('total_general', models.DecimalField(max_digits=7, decimal_places=2, default=0)),
                ('term1_average', models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)),
                ('term2_average', models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)),
                ('term3_average', models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)),
                ('annual_average',  models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)),
                ('annual_position', models.PositiveIntegerField(null=True, blank=True)),
                ('days_present', models.PositiveIntegerField(default=0)),
                ('days_absent',  models.PositiveIntegerField(default=0)),
                ('days_late',    models.PositiveIntegerField(default=0)),
                ('conduct', models.CharField(max_length=10, blank=True,
                    choices=[('good','Good'),('fair','Fair'),('poor','Poor'),('excellent','Excellent')])),
                ('class_teacher_remark', models.TextField(blank=True)),
                ('head_teacher_remark',  models.TextField(blank=True)),
                ('promotion_status', models.CharField(
                    max_length=10,
                    choices=[('promoted','Promoted'),('repeat','Repeat'),('dismissed','Dismissed'),('pending','Pending')],
                    default='pending'
                )),
                ('promoted_to_class',  models.CharField(max_length=50, blank=True)),
                ('next_term_begins',   models.DateField(null=True, blank=True)),
                ('discipline_brutal',  models.BooleanField(default=False)),
                ('absenteisme',        models.BooleanField(default=False)),
                ('barvade',            models.BooleanField(default=False)),
                ('pdf_file',     models.FileField(upload_to='report_cards/%Y/%m/', null=True, blank=True)),
                ('generated_at', models.DateTimeField(null=True, blank=True)),
                ('created_at',   models.DateTimeField(auto_now_add=True)),
                ('updated_at',   models.DateTimeField(auto_now=True)),
                ('is_finalised', models.BooleanField(default=False)),
                ('student', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='report_cards',
                    to='students.student'
                )),
                ('term', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='report_cards',
                    to='academics.academicterm'
                )),
                ('school_class', models.ForeignKey(
                    on_delete=django.db.models.deletion.CASCADE,
                    related_name='report_cards',
                    to='academics.schoolclass'
                )),
            ],
            options={
                'verbose_name': 'Report Card',
                'verbose_name_plural': 'Report Cards',
                'ordering': ['-term__academic_year', 'term__term', 'student__last_name'],
                'unique_together': {('student', 'term')},
                'indexes': [
                    models.Index(fields=['student', 'term'],       name='rc_student_term_idx'),
                    models.Index(fields=['school_class', 'term'],  name='rc_class_term_idx'),
                    models.Index(fields=['report_type'],           name='rc_report_type_idx'),
                ],
            },
        ),
    ]