from .base_expert import BaseExpert


class HandwritingExpert(BaseExpert):
    def __init__(self):
        super().__init__()
        self.expert_id = "A8"
        self.expert_name = "Handwriting (Kinesthetic Progression)"

    def get_json_schema(self, q, a, **_kwargs):
        q_safe = q.replace('"', "'").replace("\n", " ")
        a_safe = a.replace('"', "'").replace("\n", " ")
        return (
            "HANDWRITING PEDAGOGY (Kinesthetic Progression):\n"
            "Follow the progression: Air writing -> Finger tracing -> Pencil use.\n"
            "Separate motor skill development from cognitive letter recognition.\n\n"
            "STRICT RULES:\n"
            "1. GATED READ: Node 2 is MCQ with exactly one option 'READY - I have read it'.\n"
            "2. KINESTHETIC STEP: Add a STORY node instructing the student to trace the word/shape in the air with their finger before writing.\n"
            "3. STROKE ORDER: Add a 'reorder' type QUESTION where the student must arrange the strokes of the word/letter in the correct order.\n"
            "4. SPACING & LEGIBILITY: Add a fill_blank question asking the student to type the word while paying attention to spacing.\n"
            "5. HINTS: Every QUESTION must have a real hint.\n"
            "6. FULL RECALL: Ask the student to write the complete text on paper and then type it here.\n"
            "7. GRADING: Final MCQ with options 'Again (1)', 'Hard (3)', 'Good (4)', 'Easy (5)'.\n"
            "8. Every QUESTION must have TWO outgoing edges: correct -> next, wrong -> itself.\n"
            "9. Output ONLY valid JSON. No markdown fences. No commentary.\n\n"
            "REQUIRED SHAPE:\n"
            '{\n'
            '  "nodes": [\n'
            '    {"id": 1, "type": "START", "label": "Begin", "config": {}},\n'
            '    {"id": 2, "type": "QUESTION", "label": "Read", "config": {"type": "mcq", "text": "READ CAREFULLY:\\n\\nQ: ' + q_safe + '\\n\\nA: ' + a_safe + '", "options": ["READY - I have read it"], "correct_answer": "READY - I have read it", "hint": "Click READY"}},\n'
            '    {"id": 3, "type": "STORY", "label": "Kinesthetic Step", "config": {"title": "Air Writing", "body": "Trace the letters of the answer in the air with your finger. Feel the strokes."}},\n'
            '    {"id": 4, "type": "QUESTION", "label": "Stroke Order", "config": {"type": "reorder", "text": "Arrange the strokes in the correct order", "options": ["Stroke 1", "Stroke 2", "Stroke 3"], "correct_answer": "Stroke 1 Stroke 2 Stroke 3", "hint": "Start from top-left"}},\n'
            '    {"id": 5, "type": "QUESTION", "label": "Spacing Check", "config": {"type": "fill_blank", "text": "Type the answer carefully, paying attention to spacing.", "correct_answer": "' + a_safe + '", "hint": "Notice the gaps between words"}},\n'
            '    {"id": 50, "type": "QUESTION", "label": "Full Recall", "config": {"type": "fill_blank", "text": "Write the COMPLETE answer on paper, then type it here from memory:\\n\\n' + q_safe + '", "correct_answer": "' + a_safe + '", "hint": "Recall the full structure"}},\n'
            '    {"id": 60, "type": "QUESTION", "label": "Grading", "config": {"type": "mcq", "text": "How hard was this?", "options": ["Again (1)", "Hard (3)", "Good (4)", "Easy (5)"], "correct_answer": "Good (4)", "hint": "Be honest"}},\n'
            '    {"id": 61, "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},\n'
            '    {"from": 2, "to": 2, "condition": "wrong", "priority": 2},\n'
            '    {"from": 3, "to": 4, "condition": "", "priority": 1},\n'
            '    {"from": 4, "to": 5, "condition": "correct", "priority": 1},\n'
            '    {"from": 4, "to": 4, "condition": "wrong", "priority": 2},\n'
            '    {"from": 5, "to": 50, "condition": "correct", "priority": 1},\n'
            '    {"from": 5, "to": 5, "condition": "wrong", "priority": 2},\n'
            '    {"from": 50, "to": 60, "condition": "correct", "priority": 1},\n'
            '    {"from": 50, "to": 50, "condition": "wrong", "priority": 2},\n'
            '    {"from": 60, "to": 61, "condition": "correct", "priority": 1}\n'
            '  ]\n'
            '}\n'
        )
