import re
from ..base_expert import BaseExpert


class UnderstandingExpert(BaseExpert):
    def __init__(self):
        super().__init__()
        self.expert_id = "U0"
        self.expert_name = "Understanding Panel (Story-Based)"

    def _sanitize(self, text):
        if not text:
            return ""
        text = re.sub(r'<[^>]+>', '', text)
        text = text.replace('&#x000D7;', 'x').replace('&#x0003D;', '=').replace('&#x0002E;', '.')
        return re.sub(r'\s+', ' ', text).strip()

    def get_json_schema(self, q, a, medium='EN', tier=2):
        q_safe = self._sanitize(q).replace('"', "'").replace("\n", " ")
        a_safe = self._sanitize(a).replace('"', "'").replace("\n", " ")
        return (
            "TASK: Return ONE JSON object for an UNDERSTANDING-ONLY story flow.\n"
            "CONTENT LANGUAGE: ROMAN URDU ONLY. No English in any student-facing field.\n"
            "Student is Urdu-medium. All narratives, questions, hints, options in Roman Urdu.\n\n"
            "STRUCTURE: 12 nodes = 1 START + 1 Hook + 3 Story + 3 QAR + 1 Misconception + 1 Transfer + 1 Grade + 1 END.\n\n"
            "8-EXPERT PANEL: U1 Story Architect, U2 Character Designer, U3 Dialogue Writer, U4 Visual Director, U5 Cultural Consultant, U6 Gamification, U7 Misconception Strategist, U8 Assessment Alchemist.\n\n"
            "CONTENT FIELDS: Use ONLY question_ur, answer_ur, hint_ur. NO English fields.\n"
            "Examples of good Roman Urdu:\n"
            "  question_ur: 'Yeh kahani ek larke ki hai jo cricket khelta hai...'\n"
            "  answer_ur: 'Us ne socha ke agar...'\n"
            "  hint_ur: 'Asal usool yaad karo'\n\n"
            "STORY PHASE (all type=read):\n"
            "  Hook (id=2): question_ur='[Urdu hook title]', answer_ur='[1-2 sentence scenario]'.\n"
            "  Story-1 (id=3): question_ur='BAB 1: [Urdu title]', answer_ur='[3-5 sentence narrative]'.\n"
            "  Story-2 (id=4): question_ur='BAB 2: [Urdu title]', answer_ur='[narrative with analogy]'.\n"
            "  Story-3 (id=5): question_ur='BAB 3: [Urdu title]', answer_ur='[hero wins]'.\n\n"
            "QAR PHASE (all type=mcq):\n"
            "  QAR-1 (id=6): question_ur='Kahani ke mutabiq [character] ne kya kiya?'\n"
            "    options=[4 Roman Urdu strings], correct_answer=[Roman Urdu], hint_ur='BAB 2 dekho'.\n"
            "  QAR-2 (id=7): question_ur='Yeh kyun hua?'\n"
            "    options=[4 Roman Urdu], correct_answer=[Roman Urdu].\n"
            "  QAR-3 (id=8): question_ur='Agar tum [character] hote to kya karte?'\n"
            "    options=[4 Roman Urdu], correct_answer=[Roman Urdu].\n\n"
            "MISCONCEPTION (id=9, type=read):\n"
            "  question_ur='Aam Ghalti', answer_ur='Bohut log samajhte hain ke [X], asal mein [Y]. Kahani mein jab...'\n\n"
            "TRANSFER (id=10, type=fill_blank):\n"
            "  question_ur='Is concept ko NAYI situation mein lagao: [new scenario]. Tum kya karte?'\n"
            "  hint_ur='Asal usool socho', correct_answer=[Roman Urdu 1-2 sentences], correct_answer_alt=[same].\n"
            "  NO word_bank, NO passage.\n\n"
            "GRADE (id=11, type=mcq):\n"
            "  question_ur='Kya tum confident ho is concept ko lagane mein?'\n"
            "  options=['Abhi nahi', 'Kuch had tak', 'Confident', 'Bohut zyada'].\n\n"
            "OUTPUT: ONLY valid JSON, no markdown fences.\n\n"
            "SHAPE:\n"
            "{\n"
            '  "nodes": [\n'
            '    {"id": 1, "type": "START", "label": "Begin", "config": {}},\n'
            '    {"id": 2, "type": "QUESTION", "label": "Hook", "config": {"type": "read", "question_ur": "[Hook title in Roman Urdu]", "answer_ur": "[scenario in Roman Urdu]", "hint_ur": "READY dabayein"}},\n'
            '    {"id": 3, "type": "QUESTION", "label": "Story-1", "config": {"type": "read", "question_ur": "BAB 1: [Urdu title]", "answer_ur": "[narrative in Roman Urdu]"}},\n'
            '    {"id": 4, "type": "QUESTION", "label": "Story-2", "config": {"type": "read", "question_ur": "BAB 2: [Urdu title]", "answer_ur": "[analogy in Roman Urdu]"}},\n'
            '    {"id": 5, "type": "QUESTION", "label": "Story-3", "config": {"type": "read", "question_ur": "BAB 3: [Urdu title]", "answer_ur": "[hero wins in Roman Urdu]"}},\n'
            '    {"id": 6, "type": "QUESTION", "label": "QAR-1", "config": {"type": "mcq", "question_ur": "[Right There question]", "options": ["[opt1]", "[opt2]", "[opt3]", "[opt4]"], "correct_answer": "[correct]", "hint_ur": "BAB 2 dekho"}},\n'
            '    {"id": 7, "type": "QUESTION", "label": "QAR-2", "config": {"type": "mcq", "question_ur": "[Think Search question]", "options": ["[opt1]", "[opt2]", "[opt3]", "[opt4]"], "correct_answer": "[correct]", "hint_ur": "BAB 1 aur 3 jodo"}},\n'
            '    {"id": 8, "type": "QUESTION", "label": "QAR-3", "config": {"type": "mcq", "question_ur": "[Author Me question]", "options": ["[best]", "[okay]", "[wrong]", "[worst]"], "correct_answer": "[best]", "hint_ur": "Kahani ka usool"}},\n'
            '    {"id": 9, "type": "QUESTION", "label": "Misconception", "config": {"type": "read", "question_ur": "Aam Ghalti", "answer_ur": "[misconception explanation]"}},\n'
            '    {"id": 10, "type": "QUESTION", "label": "Transfer", "config": {"type": "fill_blank", "question_ur": "[new scenario + question]", "hint_ur": "[hint]", "correct_answer": "[Roman Urdu answer]", "correct_answer_alt": "[same answer]"}},\n'
            '    {"id": 11, "type": "QUESTION", "label": "Grade", "config": {"type": "mcq", "question_ur": "Kya tum confident ho is concept ko lagane mein?", "options": ["Abhi nahi", "Kuch had tak", "Confident", "Bohut zyada"], "correct_answer": "Confident"}},\n'
            '    {"id": 12, "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'
            '  ]\n'
            '}\n'
        )
