"""
academics/management/commands/populate_report_subjects.py

Run once after migrations:
    python manage.py populate_report_subjects

This creates the complete ReportSubject catalogue for all 4 formats,
exactly matching the scanned report card templates.
"""
from django.core.management.base import BaseCommand


# ── Subject data ────────────────────────────────────────────────────────────
# Format: (report_type, domain, activity, sub_activity, order)

SUBJECTS = [

    # =========================================================
    # ANGLOPHONE PRIMARY  (ap)  — doc 13
    # Structure: ACTIVITIES  >  SUB-ACTIVITIES
    # Marks /20 per month;  columns: 1st-3rd Month, Synthese, Appreciation
    # =========================================================
    ('ap', 'MATHEMATICS',              '',                  'Paper 1',                        10),
    ('ap', 'MATHEMATICS',              '',                  'Paper 2',                        11),
    ('ap', 'ENGLISH LANGUAGE',         '',                  'Writing',                        20),
    ('ap', 'ENGLISH LANGUAGE',         '',                  'Dictation',                      21),
    ('ap', 'ENGLISH LANGUAGE',         '',                  'Guided Writing',                 22),
    ('ap', 'ENGLISH LANGUAGE',         '',                  'Composition',                    23),
    ('ap', 'ENGLISH LANGUAGE',         '',                  'Grammar/Vocabulary',             24),
    ('ap', 'FRENCH LANGUAGE',          '',                  'Expression Orale',               30),
    ('ap', 'FRENCH LANGUAGE',          '',                  'Grammaire/Vocabulaire',          31),
    ('ap', 'FRENCH LANGUAGE',          '',                  'Productions d\'écrit',           32),
    ('ap', 'FRENCH LANGUAGE',          '',                  'Littérature',                    33),
    ('ap', 'SCIENCE AND TECHNOLOGY',   '',                  'Health Education',               40),
    ('ap', 'SCIENCE AND TECHNOLOGY',   '',                  'Environ. Education',             41),
    ('ap', 'SCIENCE AND TECHNOLOGY',   '',                  'Technology & Engineering',       42),
    ('ap', 'ICT',                      '',                  'ICT',                            50),
    ('ap', 'SOCIAL STUDIES',           '',                  'History',                        60),
    ('ap', 'SOCIAL STUDIES',           '',                  'Geography',                      61),
    ('ap', 'SOCIAL STUDIES',           '',                  'Citizenship/Civics',             62),
    ('ap', 'SOCIAL STUDIES',           '',                  'Moral Education',                63),
    ('ap', 'SOCIAL STUDIES',           '',                  'Human Rights',                   64),
    ('ap', 'VOCATIONAL STUDIES',       '',                  'Agro/Pastoral',                  70),
    ('ap', 'VOCATIONAL STUDIES',       '',                  'Home Economics',                 71),
    ('ap', 'VOCATIONAL STUDIES',       '',                  'Crafts',                         72),
    ('ap', 'ARTS',                     '',                  'Music',                          80),
    ('ap', 'ARTS',                     '',                  'Literary Arts',                  81),
    ('ap', 'ARTS',                     '',                  'Performance Arts',               82),
    ('ap', 'SPORTS & PHYSICAL EDUCATION', '',               'Written',                        90),
    ('ap', 'SPORTS & PHYSICAL EDUCATION', '',               'Practical',                      91),
    ('ap', 'NATIONAL LANGUAGE/CULTURE',   '',               'National Language',             100),
    ('ap', 'NATIONAL LANGUAGE/CULTURE',   '',               'National Culture',              101),

    # =========================================================
    # ANGLOPHONE NURSERY  (an)  — doc 14
    # Structure: DOMAINS > ACTIVITIES > SUB-ACTIVITIES
    # Marks /20; 9 months across 3 terms
    # =========================================================

    # LITERACY AND COMMUNICATION
    ('an', 'LITERACY AND COMMUNICATION', 'STORY TELLING',        'Listen to Short Story',           10),
    ('an', 'LITERACY AND COMMUNICATION', 'STORY TELLING',        'Identify Characters in a Story',  11),
    ('an', 'LITERACY AND COMMUNICATION', 'STORY TELLING',        'Chant Stories',                   12),
    ('an', 'LITERACY AND COMMUNICATION', 'RHYMES/POETRY',        'Listen to Poems and Rhymes',      20),
    ('an', 'LITERACY AND COMMUNICATION', 'RHYMES/POETRY',        'Recite Poems and Rhymes',         21),
    ('an', 'LITERACY AND COMMUNICATION', 'RHYMES/POETRY',        'Perform',                         22),
    ('an', 'LITERACY AND COMMUNICATION', 'SIGN LANGUAGE/GESTURES','Non Verbal Communication',       30),
    ('an', 'LITERACY AND COMMUNICATION', 'SIGN LANGUAGE/GESTURES','Mime & Mimic Various Occupation',31),
    ('an', 'LITERACY AND COMMUNICATION', 'READING',              'Read Pictures',                   40),
    ('an', 'LITERACY AND COMMUNICATION', 'READING',              'Listen to Short Stories',         41),
    ('an', 'LITERACY AND COMMUNICATION', 'READING',              'Read Words Aloud',                42),
    ('an', 'LITERACY AND COMMUNICATION', 'READING',              'Recognize Letter Sounds',         43),
    ('an', 'LITERACY AND COMMUNICATION', 'WRITING',              'Reproduce Models',                50),
    ('an', 'LITERACY AND COMMUNICATION', 'WRITING',              'Write Numbers (1-10)',            51),
    ('an', 'LITERACY AND COMMUNICATION', 'WRITING',              'Write Letters of the Alphabet',   52),

    # SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'MATEMATICS', 'Count Numbers (from 1-10)',       60),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'MATEMATICS', 'Write Numbers from 1-5',         61),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'MATEMATICS', 'Sensory & Objects According to Colours', 62),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'MATEMATICS', 'Add and Subtract',                63),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'MATEMATICS', 'Identify Various Shapes',         64),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'ICT',        'Identify a Computer System',      70),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'SENSORY & PERCEPTIVE EDUCATION', 'Identify Primary Colour', 80),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'SENSORY & PERCEPTIVE EDUCATION', 'Distinguish Taste',       81),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'SENSORY & PERCEPTIVE EDUCATION', 'React to and External Stimulus', 82),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'SCIENCES AND TECHNOLOGIES', 'Describe Day and Night',      90),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'SCIENCES AND TECHNOLOGIES', 'Name Sources of Light',       91),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'AGRICULTURE', 'Identify Plants',                100),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'AGRICULTURE', 'Plant Seeds and Seedlings',      101),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'MOTOR ACTS',  'Gymnastics',                    110),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'MOTOR ACTS',  'Athletics',                     111),
    ('an', 'SCIENCE AND TECHNOLOGICAL SKILLS DEVELOPMENT', 'MOTOR ACTS',  'Collective Games',              112),

    # PRACTICAL LIFE SKILLS
    ('an', 'PRACTICAL LIFE SKILLS', 'CITIZENSHIP',         'Recognise Self',                    120),
    ('an', 'PRACTICAL LIFE SKILLS', 'CITIZENSHIP',         'Identify Key State and Local Officials', 121),
    ('an', 'PRACTICAL LIFE SKILLS', 'CITIZENSHIP',         'Show love for Self and Others',     122),
    ('an', 'PRACTICAL LIFE SKILLS', 'CHARACTER EDUCATION', 'Share with Peers',                  130),
    ('an', 'PRACTICAL LIFE SKILLS', 'CHARACTER EDUCATION', 'Conform to Simple Etiquettes',      131),
    ('an', 'PRACTICAL LIFE SKILLS', 'ENVIRONMENTAL EDUCATION', 'Dispose Waste Property',        140),
    ('an', 'PRACTICAL LIFE SKILLS', 'ENVIRONMENTAL EDUCATION', 'Identify Weather Conditions',   141),
    ('an', 'PRACTICAL LIFE SKILLS', 'SAFETY EDUCATION',    'Recognise Dangerous Objects',       150),
    ('an', 'PRACTICAL LIFE SKILLS', 'SAFETY EDUCATION',    'Comply With Safety Rules at Home, School', 151),
    ('an', 'PRACTICAL LIFE SKILLS', 'HEALTH EDUCATION',    'Reproduce Models',                  160),
    ('an', 'PRACTICAL LIFE SKILLS', 'HEALTH EDUCATION',    'Demonstrate Healthy Eating Habits', 161),
    ('an', 'PRACTICAL LIFE SKILLS', 'HEALTH EDUCATION',    'Identify a First Aid Box',          162),
    ('an', 'PRACTICAL LIFE SKILLS', 'HEALTH EDUCATION',    'Use Appropriate Posture',           163),

    # ARTS AND CRAFT
    ('an', 'ARTS AND CRAFT', 'MUSIC AND DANCE',      'Present a Song',                    170),
    ('an', 'ARTS AND CRAFT', 'MUSIC AND DANCE',      'Display Cameroonian Dance',         171),
    ('an', 'ARTS AND CRAFT', 'DRAWING AND COLOURING','Identify Colours',                  180),
    ('an', 'ARTS AND CRAFT', 'DRAWING AND COLOURING','Draw an Image',                     181),
    ('an', 'ARTS AND CRAFT', 'DRAWING AND COLOURING','Use at least Two Colours',          182),
    ('an', 'ARTS AND CRAFT', 'PAINTING',             'Use a Painting Brush',              190),
    ('an', 'ARTS AND CRAFT', 'PAINTING',             'Mix Colours',                       191),
    ('an', 'ARTS AND CRAFT', 'HANDWORK',             'Identify Materials',                200),
    ('an', 'ARTS AND CRAFT', 'HANDWORK',             'Produce a Design',                  201),

    # =========================================================
    # FRANCOPHONE PRIMARY  (fp)  — doc 11
    # Structure: DOMAINES > SOUS-ACTIVITÉS
    # Marks /20; scale NA/ECA/A/A+
    # =========================================================
    ('fp', 'FRANCAIS',              '',  'Dictée et Question',               10),
    ('fp', 'FRANCAIS',              '',  'Production d\'écrits',             11),
    ('fp', 'FRANCAIS',              '',  'Littérature',                      12),
    ('fp', 'FRANCAIS',              '',  'Grammaire',                        13),
    ('fp', 'FRANCAIS',              '',  'Orthographe',                      14),
    ('fp', 'FRANCAIS',              '',  'Vocabulaire/Etude de texte',       15),
    ('fp', 'FRANCAIS',              '',  'Conjugaison',                      16),
    ('fp', 'MATHS',                 '',  'Calcul Rapide',                    20),
    ('fp', 'MATHS',                 '',  'Problèmes',                        21),
    ('fp', 'QUESTIONS DE COURS',    '',  'Science et technologies',          30),
    ('fp', 'QUESTIONS DE COURS',    '',  'Science Humaines et Sociales',     31),
    ('fp', 'QUESTIONS DE COURS',    '',  'Histoire',                         32),
    ('fp', 'QUESTIONS DE COURS',    '',  'Géographie',                       33),
    ('fp', 'QUESTIONS DE COURS',    '',  'TIC',                              34),
    ('fp', 'ENGLISH LANGUAGE',      '',  'Grammar & Vocabulary',             40),
    ('fp', 'ENGLISH LANGUAGE',      '',  'Reading Comprehension',            41),
    ('fp', 'ENGLISH LANGUAGE',      '',  'Dictation',                        42),
    ('fp', 'ENGLISH LANGUAGE',      '',  'Speaking/Listening',               43),
    ('fp', 'EDUCATION ARTISTIQUE',  '',  'Art Visuel/Lang. & Cult. Nat.',    50),
    ('fp', 'EDUCATION ARTISTIQUE',  '',  'Musique',                          51),
    ('fp', 'EDUCATION ARTISTIQUE',  '',  'Art Dramatiques/Dessins',          52),
    ('fp', 'EDUCATION ARTISTIQUE',  '',  'Danse',                            53),
    ('fp', 'EPS',                   '',  'Activités Athlétiques',            60),
    ('fp', 'EPS',                   '',  'Sports Collectif',                 61),
    ('fp', 'DEVEL PERSO',           '',  'Artisanat et construction artistique', 70),
    ('fp', 'DEVEL PERSO',           '',  'Activités Agropastorales',         71),
    ('fp', 'DEVEL PERSO',           '',  'Activités domestique et Familiale',72),
    ('fp', 'EPREUVES ORALES',       '',  'Lecture',                          80),
    ('fp', 'EPREUVES ORALES',       '',  'Récitation',                       81),
    ('fp', 'EPREUVES ORALES',       '',  'Chant',                            82),

    # =========================================================
    # FRANCOPHONE NURSERY  (fn)  — doc 12
    # Structure: ACTIVITÉS > SOUS-ACTIVITÉS
    # =========================================================
    ('fn', 'LANGUE ET COMMUNICATION',            '', 'Expression Orale',                         10),
    ('fn', 'LANGUE ET COMMUNICATION',            '', 'Graphisme',                                11),
    ('fn', 'LANGUE ET COMMUNICATION',            '', 'Lecture Ecriture',                         12),
    ('fn', 'LANGUE ET COMMUNICATION',            '', 'Anglais',                                  13),
    ('fn', 'LANGUE ET COMMUNICATION',            '', 'Orthographe',                              14),
    ('fn', 'LANGUE ET COMMUNICATION',            '', 'Langues Nationales',                       15),
    ('fn', 'LANGUE ET COMMUNICATION',            '', 'Expression Gestuelle',                     16),
    ('fn', 'EVEIL SCIENTIFIQUE ET TECHNOLOGIQUE','', 'Mathématiques',                             20),
    ('fn', 'EVEIL SCIENTIFIQUE ET TECHNOLOGIQUE','', 'Education Personnelle et perceptive',       21),
    ('fn', 'EVEIL SCIENTIFIQUE ET TECHNOLOGIQUE','', 'Science et technologies',                  22),
    ('fn', 'EVEIL SCIENTIFIQUE ET TECHNOLOGIQUE','', 'TIC',                                      23),
    ('fn', 'EVEIL SCIENTIFIQUE ET TECHNOLOGIQUE','', 'Activités agro-Pastorales',                24),
    ('fn', 'VIE COURANTE',                       '', 'Education Civique et Morale',              30),
    ('fn', 'VIE COURANTE',                       '', 'Langage des Signes',                       31),
    ('fn', 'VIE COURANTE',                       '', 'Education à la conte et à la nutrition',   32),
    ('fn', 'VIE COURANTE',                       '', 'Education à la Sécurité',                  33),
    ('fn', 'VIE COURANTE',                       '', 'Education à l\'Environnement',             34),
    ('fn', 'CREATION ARTISTIQUE ET ACTIVITE MANUELLE', '', 'Peinture',                           40),
    ('fn', 'CREATION ARTISTIQUE ET ACTIVITE MANUELLE', '', 'Dessin et Coloriage',                41),
    ('fn', 'CREATION ARTISTIQUE ET ACTIVITE MANUELLE', '', 'Activités Manuelles',                42),
    ('fn', 'CREATION ARTISTIQUE ET ACTIVITE MANUELLE', '', 'Musique, Chant et Danse',            43),
    ('fn', 'MOTRICITE GENERALE',                 '', 'Gymnastique',                              50),
    ('fn', 'MOTRICITE GENERALE',                 '', 'Poetry Rhyme',                             51),
    ('fn', 'MOTRICITE GENERALE',                 '', 'Athlétisme',                               52),
    ('fn', 'MOTRICITE GENERALE',                 '', 'Jeux Collectifs',                          53),
]


class Command(BaseCommand):
    help = 'Populate the ReportSubject table for all 4 report card formats.'

    def handle(self, *args, **options):
        from academics.models_grades import ReportSubject

        created_count = 0
        skipped_count = 0

        for (rtype, domain, activity, sub_activity, order) in SUBJECTS:
            obj, created = ReportSubject.objects.get_or_create(
                report_type=rtype,
                domain=domain,
                activity=activity,
                sub_activity=sub_activity,
                defaults={'order': order}
            )
            if created:
                created_count += 1
            else:
                skipped_count += 1

        self.stdout.write(self.style.SUCCESS(
            f'✅ Done! Created {created_count} subjects, '
            f'skipped {skipped_count} existing records.'
        ))
