"""Academy roster seed - idempotent, complete (KG + 1-10 + 9th import).

Teacher directive 2026-10-15: ONE attendance + fee page for the entire
Academy (KG, Class 1-10), including the existing 9th-class cohort.

Standards:
  ISO 8000-110 (uniqueness) · ISO 8601 (year cohort)
  Rule 9 (append-only) · Rule 10 (one canonical writer)
  Rule 24 (sandbox first) · Rule 28 (standards supremacy)

Every row: code = IDA-2026-NNNN (unique, no future collision).
9th-class rows carry linked_code = their exams.Student.code (S-####)
so the exams system can look them up read-only later.
"""
from django.core.management.base import BaseCommand
from django.db import transaction

from academy.models import AcademyStudent

COHORT = 2026
PREFIX = f"IDA-{COHORT}-"
ACADEMY_ONLY = [(0, 'Nain Tara'), (2, 'Dua Fatima'), (2, 'Ajwa Noor'), (2, 'Anaya Qamar'), (2, 'Areeha'), (2, 'Eman Azam'), (2, 'Umm-e-Habiba'), (1, 'Sageer'), (1, 'Ayesha Naeem'), (3, 'Abdul Moiz'), (3, 'Saim'), (4, 'Gulshan Nasir'), (4, 'Faiza Mehdi'), (4, 'Abdul Hadi'), (4, 'Habibullah'), (4, 'Hadi Afzal'), (5, 'Noor Azam'), (5, 'Abdul Kabir'), (5, 'Yousuf'), (5, 'Mujahid'), (6, 'Amna Faisal'), (6, 'Ayesha Mazhar'), (6, 'Noor Arshad'), (6, 'Ayesha Parwaiz'), (6, 'Areeba'), (6, 'Bushra Rehman'), (6, 'Hadi Hanif'), (6, 'Hanif'), (6, 'Muhammad Rahim'), (6, 'Ahmad Ali'), (6, 'Qanzil Iman'), (6, 'Abid Raza'), (7, 'Arbab Faisal'), (7, 'Ayat Fatima'), (10, 'Ayesha Nasir'), (10, 'Aiman Afzal'), (10, 'Ibrahim Noor'), (10, 'Shamyan')]


class Command(BaseCommand):
    help = "Seed AcademyStudent rows (complete Academy roster)."

    @transaction.atomic
    def handle(self, *args, **opts):
        # Only reset IDA-* rows. Anything else (legacy A-*) is left alone.
        AcademyStudent.objects.filter(code__regex=r'^IDA-').delete()

        n = 0
        # 1) Academy-only students (KG + Classes 1-10 minus 9)
        for grade, name in ACADEMY_ONLY:
            n += 1
            AcademyStudent.objects.create(
                code=f"{PREFIX}{n:04d}",
                full_name=name,
                grade=grade,
                default_fee_amount=0,
                active=True,
                linked_code="",
            )

        # 2) 9th-class cohort imported read-only from exams.Student
        imported = 0
        try:
            from exams.models import Student as ExamStudent
            for s in ExamStudent.objects.order_by("code"):
                n += 1
                name = (getattr(s, "full_name", None)
                        or getattr(s, "name", None)
                        or s.code)
                AcademyStudent.objects.create(
                    code=f"{PREFIX}{n:04d}",
                    full_name=name,
                    grade=9,
                    default_fee_amount=0,
                    active=True,
                    linked_code=s.code,
                )
                imported += 1
        except Exception as e:
            self.stderr.write(f"9th import failed: {e}")

        self.stdout.write(
            f"created={len(ACADEMY_ONLY)} academy_only + {imported} ninth "
            f"= {n} total  prefix={PREFIX}"
        )
