from .base_expert import BaseExpert


class ComputerScienceExpert(BaseExpert):
    def __init__(self):
        super().__init__()
        self.expert_id = "A7"
        self.expert_name = "Computer Science (Parsons Problems)"

    def get_json_schema(self, q, a, **_kwargs):
        q_safe = q.replace('"', "'").replace("\n", " ")
        a_safe = a.replace('"', "'").replace("\n", " ")
        return (
            "COMPUTER SCIENCE PEDAGOGY (Parsons Problems + Worked Examples):\n"
            "Use Parsons Problems (code reordering) before asking the student to write code from scratch.\n\n"
            "STRICT RULES:\n"
            "1. GATED READ: Node 2 is MCQ with exactly one option 'READY - I have read it'.\n"
            "2. WORKED EXAMPLE: Present a STORY node with a solved example the student can study.\n"
            "3. PARSONS PROBLEM: Add a 'reorder' type QUESTION that gives scrambled code lines (options array) that the student must arrange in the correct execution order (correct_answer is the space-joined correct order).\n"
            "4. INCLUDE DISTRACTORS: Add 1-2 incorrect code blocks to the reorder options that must be ignored.\n"
            "5. CUED RECALL: Every keyword node must be type 'fill_blank'. Blank out ONE keyword from the answer text.\n"
            "6. HINTS: Every QUESTION must have a real hint.\n"
            "7. FULL RECALL: Ask the student to write the COMPLETE answer from memory.\n"
            "8. GRADING: Final MCQ with options 'Again (1)', 'Hard (3)', 'Good (4)', 'Easy (5)'.\n"
            "9. Every QUESTION must have TWO outgoing edges: correct -> next, wrong -> itself.\n"
            "10. 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": "Worked Example", "config": {"title": "Study this example", "body": "[A complete, commented example of the concept]"}},\n'
            '    {"id": 4, "type": "QUESTION", "label": "Parsons Problem", "config": {"type": "reorder", "text": "Arrange the code in correct execution order. Ignore the incorrect blocks.", "options": ["line1", "line2", "line3", "WRONG_DISTRACTOR"], "correct_answer": "line1 line2 line3", "hint": "Think about the execution order"}},\n'
            '    {"id": 5, "type": "QUESTION", "label": "Keyword 1", "config": {"type": "fill_blank", "text": "Fill the blank:\\n\\n' + a_safe + '", "correct_answer": "THE_KEYWORD", "hint": "Starts with X"}},\n'
            '    ... one fill_blank node per keyword (2-4 nodes) ...\n'
            '    {"id": 50, "type": "QUESTION", "label": "Full Recall", "config": {"type": "fill_blank", "text": "Write the COMPLETE answer 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'
            '    ... sequential edges for each keyword node ...\n'
            '    {"from": <last_keyword_id>, "to": 50, "condition": "correct", "priority": 1},\n'
            '    {"from": <last_keyword_id>, "to": <last_keyword_id>, "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'
        )
