class BaseExpert:
    def __init__(self):
        self.expert_id = "A0"
        self.expert_name = "Base"

    def get_prompt(self, question, subject, tier=2, medium='EN', pasted_answer=None):
        q = (question.question_text or "").strip()
        a_orig = (question.model_answer or "").strip()
        a_over = (pasted_answer or "").strip()
        a = a_over or a_orig
        if medium not in ('EN', 'UR', 'UD'):
            medium = 'EN'
        try:
            tier = int(tier)
        except (ValueError, TypeError):
            tier = 2
        if tier not in (1, 2, 3):
            tier = 1
        hdr = (
            "ROLE: Expert " + self.expert_id + " (" + self.expert_name + ")\n"
            "SUBJECT: " + str(subject) + "\n"
            "STUDENT TIER: " + str(tier) + "\n"
            "MEDIUM: " + ("English" if medium == "EN" else ("Urdu" if medium == "UD" else "Roman Urdu")) + "\n"
            "QUESTION: " + q + "\n"
            "MODEL ANSWER: " + a + "\n"
        )
        if a_over:
            hdr += "SOURCE NOTE: pasted answer is canonical. Use verbatim.\n"
        if medium == "UD":
            hdr += "STRICT RULE: The output MUST be written ONLY in pure Urdu script (اردو). Do NOT use any English or Roman Urdu words. Do NOT use English digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). You MUST use ONLY Urdu digits (۰, ۱, ۲, ۳, ۴, ۵, ۶, ۷, ۸, ۹) for all numbers. Translate all concepts and instructions into pure Urdu.\n"
        final_prompt = hdr + "\n" + self.get_json_schema(q, a, medium=medium, tier=tier)
        if medium == "UD":
            final_prompt += "\n\nMASTER OVERRIDE: IGNORE ANY PREVIOUS INSTRUCTIONS ABOUT 'ROMAN URDU'. Since the medium is UD, you MUST output ALL text in pure Urdu script (نستعلیق) and use ONLY Urdu digits (۰-۹). Do NOT use any English or Roman letters/digits.\n"
        return final_prompt

    def get_json_schema(self, q, a, medium='EN', tier=2):
        q_safe = q.replace('"', "'").replace("\n", " ")
        a_safe = a.replace('"', "'").replace("\n", " ")
        if medium not in ('EN', 'UR', 'UD'):
            medium = 'EN'
        try:
            tier = int(tier)
        except (ValueError, TypeError):
            tier = 2
        if tier not in (1, 2, 3):
            tier = 1
        _en = (medium == 'EN')
        _fill = "en" if _en else "ur"
        _empty = "ur" if _en else "en"
        _tier_desc = {1: "single-keyword blanks, short passages", 2: "2-3 word phrase blanks, medium passages", 3: "multi-word clause blanks, full passages"}[tier]
        _lang_intro = (
            "MEDIUM OVERRIDE (STRICT): Student medium = " + ("English" if _en else "Roman Urdu") + ".\n"
            "  Populate ONLY the _" + _fill + " fields with real content.\n"
            "  Set EVERY _" + _empty + " field to EMPTY STRING (not translation, not placeholder).\n"
            "  word_bank chips: _en and _ur keys must both exist but the _" + _empty + " value is empty string.\n"
            "  correct_answer = " + ("English text" if _en else "empty string") + "; correct_answer_alt = " + ("empty string" if _en else "Roman Urdu text") + ".\n"
            "  NEVER mix languages. Output is single-medium only.\n"
            "TIER LEVEL " + str(tier) + ": " + _tier_desc + ".\n\n"
        )
        return (
            _lang_intro +
            "TASK: Return ONE JSON object for a MEMORIZATION-ONLY bilingual flow. No story, no understanding, only drill.\n\n"
            "STRUCTURE: 13 nodes = 1 START + 3 READ + 7 MEMORIZATION + 1 GRADE + 1 END.\n"
            "CRITICAL: Read nodes DO NOT count toward the 7 memorization steps.\n\n"
            "READ PHASE (3 nodes, NOT counted in the 7, show Q&A bilingually, tap READY each time):\n"
            "  Read-1: 'First read carefully.' Show full Q&A bilingual.\n"
            "  Read-2: 'Second read, focus on keywords.' Show full Q&A bilingual.\n"
            "  Read-3: 'Final read, then test begins.' Show full Q&A bilingual.\n\n"
            "MEMORIZATION PHASE (7 nodes):\n"
            "  1. Fill-1: Blank ONE keyword. Passage visible. Word bank 5 bilingual chips (1 correct).\n"
            "  2. Fill-2: Blank DIFFERENT keyword. Passage visible. Word bank 5 chips.\n"
            "  3. Fill-3: Blank THIRD keyword. Passage visible. Word bank 5 chips.\n"
            "  4. Reorder-1: Scramble words of sentence 1. Passage visible. Single-word chips.\n"
            "  5. Reorder-2: Scramble words of sentence 2. NO passage. Single-word chips.\n"
            "  6. Fill-4: Blank ONE important word. NO passage. Word bank of 4 single words (1 correct).\n"
            "  7. Full-Recall: Assemble the ENTIRE answer by tapping single words in the correct order. NO typing. NO passage.\n\n"
            "GLOBAL RULE: NEVER USE TEXT INPUT. Every answer must be clicked. Word bank chips MUST be exactly ONE WORD each. Never put phrases in chips.\n\n"
            "GRADE (1 node): SM-2 with options ['Again (1)', 'Hard (3)', 'Good (4)', 'Easy (5)'].\n\n"
            "BILINGUAL RULES:\n"
            "  Every question has question_en + question_ur.\n"
            "  Every passage has answer_en + answer_ur.\n"
            "  Every hint has hint_en + hint_ur.\n"
            "  Every word_bank chip = {en, ur, correct:bool}.\n"
            "  Every reorder option = {en, ur}.\n"
            "  correct_answer = English, correct_answer_alt = Roman Urdu.\n\n"
            "OUTPUT: ONLY valid JSON, no markdown fences, no commentary.\n\n"
            "SHAPE:\n"
            '{\n'
            '  "nodes": [\n'
            '    {"id": 1, "type": "START", "label": "Begin", "config": {}},\n'
            '    {"id": 2, "type": "QUESTION", "label": "Read-1", "config": {"type": "read", "question_en": "First read: ' + q_safe + '", "question_ur": "[Roman Urdu Q]", "answer_en": "' + a_safe + '", "answer_ur": "[Urdu A]"}},\n'
            '    {"id": 3, "type": "QUESTION", "label": "Read-2", "config": {"type": "read", "question_en": "Second read, focus on keywords", "question_ur": "Doosri baar padhein", "answer_en": "' + a_safe + '", "answer_ur": "[Urdu A]"}},\n'
            '    {"id": 4, "type": "QUESTION", "label": "Read-3", "config": {"type": "read", "question_en": "Final read, then test begins", "question_ur": "Aakhri read, phir test", "answer_en": "' + a_safe + '", "answer_ur": "[Urdu A]"}},\n'
            '    {"id": 5, "type": "QUESTION", "label": "Fill-1", "config": {"type": "fill_blank", "question_en": "Fill blank 1", "question_ur": "Khali 1 bharein", "answer_en": "[answer with _____]", "answer_ur": "[Urdu _____]", "hint_en": "Starts with X", "hint_ur": "X se shuru", "word_bank": [{"en":"correct","ur":"sahi","correct":true},{"en":"w1","ur":"g1","correct":false},{"en":"w2","ur":"g2","correct":false},{"en":"w3","ur":"g3","correct":false},{"en":"w4","ur":"g4","correct":false}], "correct_answer": "correct", "correct_answer_alt": "sahi"}},\n'
            '    {"id": 6, "type": "QUESTION", "label": "Fill-2", "config": {"type": "fill_blank", "question_en": "Fill blank 2", "question_ur": "Khali 2 bharein", "answer_en": "[answer with _____]", "answer_ur": "[Urdu _____]", "hint_en": "Starts with Y", "hint_ur": "Y se shuru", "word_bank": [{"en":"correct2","ur":"sahi2","correct":true},{"en":"a1","ur":"b1","correct":false},{"en":"a2","ur":"b2","correct":false},{"en":"a3","ur":"b3","correct":false},{"en":"a4","ur":"b4","correct":false}], "correct_answer": "correct2", "correct_answer_alt": "sahi2"}},\n'
            '    {"id": 7, "type": "QUESTION", "label": "Fill-3", "config": {"type": "fill_blank", "question_en": "Fill blank 3", "question_ur": "Khali 3 bharein", "answer_en": "[answer with _____]", "answer_ur": "[Urdu _____]", "hint_en": "Starts with Z", "hint_ur": "Z se shuru", "word_bank": [{"en":"correct3","ur":"sahi3","correct":true},{"en":"x1","ur":"y1","correct":false},{"en":"x2","ur":"y2","correct":false},{"en":"x3","ur":"y3","correct":false},{"en":"x4","ur":"y4","correct":false}], "correct_answer": "correct3", "correct_answer_alt": "sahi3"}},\n'
            '    {"id": 8, "type": "QUESTION", "label": "Reorder-1", "config": {"type": "reorder", "question_en": "Arrange words (sentence 1)", "question_ur": "Jumla 1 tarteeb", "answer_en": "[reference 1]", "answer_ur": "[Urdu 1]", "hint_en": "First: X", "hint_ur": "Pehla: X", "options": [{"en":"w1","ur":"l1"},{"en":"w2","ur":"l2"},{"en":"w3","ur":"l3"}], "correct_answer": "w1 w2 w3"}},\n'
            '    {"id": 9, "type": "QUESTION", "label": "Reorder-2", "config": {"type": "reorder", "question_en": "Arrange words (no reference)", "question_ur": "Alfaaz tarteeb", "hint_en": "First: X", "hint_ur": "Pehla: X", "options": [{"en":"w1","ur":"l1"},{"en":"w2","ur":"l2"},{"en":"w3","ur":"l3"}], "correct_answer": "w1 w2 w3"}},\n'
            '    {"id": 10, "type": "QUESTION", "label": "Fill-4", "config": {"type": "fill_blank", "question_en": "Fill the blank", "question_ur": "Khali jagah bharein", "answer_en": "[sentence with _____]", "answer_ur": "[Urdu sentence with _____]", "hint_en": "Starts with X", "hint_ur": "X se shuru", "word_bank": [{"en":"correct_word","ur":"sahi_lafz","correct":true},{"en":"wrong1","ur":"g1","correct":false},{"en":"wrong2","ur":"g2","correct":false},{"en":"wrong3","ur":"g3","correct":false}], "correct_answer": "correct_word", "correct_answer_alt": "sahi_lafz"}},\n'
            '    {"id": 11, "type": "QUESTION", "label": "Full-Recall", "config": {"type": "reorder", "question_en": "Assemble the complete answer", "question_ur": "Mukammal jawab tarteeb se lagayein", "hint_en": "Tap words in order", "hint_ur": "Lafzon ko tarteeb se tap karein", "options": [{"en":"word1","ur":"l1"},{"en":"word2","ur":"l2"},{"en":"word3","ur":"l3"},{"en":"word4","ur":"l4"}], "correct_answer": "' + a_safe + '", "correct_answer_alt": "[Urdu A]"}},\n'
            '    {"id": 12, "type": "QUESTION", "label": "Grade", "config": {"type": "mcq", "question_en": "How hard was this?", "question_ur": "Yeh kitna mushkil tha?", "options": ["Again (1)", "Hard (3)", "Good (4)", "Easy (5)"], "correct_answer": "Good (4)"}},\n'
            '    {"id": 13, "type": "END", "label": "Done", "config": {}}\n'
            '  ],\n'
            '  "edges": [\n'
            '    {"from": 1, "to": 2, "condition": "", "priority": 1},\n'
            '    {"from": 2, "to": 3, "condition": "correct", "priority": 1}, {"from": 2, "to": 2, "condition": "wrong", "priority": 2},\n'
            '    {"from": 3, "to": 4, "condition": "correct", "priority": 1}, {"from": 3, "to": 3, "condition": "wrong", "priority": 2},\n'
            '    {"from": 4, "to": 5, "condition": "correct", "priority": 1}, {"from": 4, "to": 4, "condition": "wrong", "priority": 2},\n'
            '    {"from": 5, "to": 6, "condition": "correct", "priority": 1}, {"from": 5, "to": 5, "condition": "wrong", "priority": 2},\n'
            '    {"from": 6, "to": 7, "condition": "correct", "priority": 1}, {"from": 6, "to": 6, "condition": "wrong", "priority": 2},\n'
            '    {"from": 7, "to": 8, "condition": "correct", "priority": 1}, {"from": 7, "to": 7, "condition": "wrong", "priority": 2},\n'
            '    {"from": 8, "to": 9, "condition": "correct", "priority": 1}, {"from": 8, "to": 8, "condition": "wrong", "priority": 2},\n'
            '    {"from": 9, "to": 10, "condition": "correct", "priority": 1}, {"from": 9, "to": 9, "condition": "wrong", "priority": 2},\n'
            '    {"from": 10, "to": 11, "condition": "correct", "priority": 1}, {"from": 10, "to": 10, "condition": "wrong", "priority": 2},\n'
            '    {"from": 11, "to": 12, "condition": "correct", "priority": 1}, {"from": 11, "to": 11, "condition": "wrong", "priority": 2},\n'
            '    {"from": 12, "to": 13, "condition": "correct", "priority": 1}, {"from": 12, "to": 12, "condition": "wrong", "priority": 2}\n'
            '  ]\n'
            '}\n'
        )
