"""KC content generation prompts — industry-standard, one intent per prompt.

Standards:
- Clear single-intent prompts (no "TASK OVERRIDE" anti-pattern)
- Structured JSON output with valid schema example
- Tier-appropriate vocabulary via style guide, not rubric concatenation
- Subject-agnostic — works for any Punjab Board subject
- Roman Urdu medium for accessibility (Sweller cognitive load theory)
"""

TIER_STYLE_GUIDE = {
    1: (
        "BASIC tier: Write for a weak student who struggles with full sentences. "
        "Use short, fragmented explanations. Include the exact textbook keywords and definitions. "
        "Use everyday examples (chai, cricket, mobile phone). The student should be able to "
        "memorize this in one pass and reproduce the keywords."
    ),
    2: (
        "STANDARD tier: Write for an average student who knows the content but rambles. "
        "Use complete grammatical sentences. Include the textbook definition + one concrete example. "
        "Use everyday examples (chai, cricket, mobile phone). Every sentence must earn its place — "
        "no filler, no repetition."
    ),
    3: (
        "ADVANCED tier: Write for a strong student who loses marks to time pressure. "
        "Use precise terminology, full conceptual explanations, and real-world applications. "
        "Include edge cases and connections to other concepts. Every word must be load-bearing. "
        "The student should be able to compress this to a 2-mark answer without losing a marking point."
    ),
}

TIER_LABEL = {1: "Basic", 2: "Standard", 3: "Advanced"}


def build_kc_prompt(subject, tier, questions_data):
    """
    Build a clean, tier-aware KC generation prompt.

    Args:
        subject: str — subject name (e.g., "General Science")
        tier: int — 1 (Basic), 2 (Standard), 3 (Advanced)
        questions_data: list of dicts with question_id, question_text, model_answer

    Returns:
        str — complete prompt ready to paste into ChatGPT
    """
    style = TIER_STYLE_GUIDE[tier]
    tier_label = TIER_LABEL[tier]

    # Build the questions block (matches the === SAWAL === format from notes page)
    sections = []
    for q in questions_data:
        sections.append(
            f"=== QUESTION ID: {q['question_id']} ===\n"
            f"Text: {q['question_text']}\n"
            f"Model Answer: {q['model_answer'] or 'N/A'}"
        )
    questions_block = "\n\n".join(sections)

    return f"""ROLE: You are a Punjab Board {subject} curriculum designer creating pedagogical
content (Knowledge Components) for {tier_label}-tier students.

STYLE GUIDE FOR {tier_label.upper()} TIER:
{style}

TASK: For each question below, generate a Knowledge Component that helps the student
UNDERSTAND the concept (not just memorize an answer). Use Roman Urdu for accessibility
(Urdu words written in English letters), but keep scientific terms in English.

REQUIRED JSON OUTPUT (valid JSON, no markdown, no explanation):
{{
  "questions": [
    {{
      "question_id": <integer — copy from the QUESTION ID above>,
      "title": "<2-4 word Urdu title for this concept>",
      "story": [
        "<Paragraph 1 — the textbook definition, written at {tier_label} depth>",
        "<Paragraph 2 — a concrete everyday example (chai/cricket/mobile)>",
        "<Paragraph 3 (optional) — why this matters in real life>"
      ],
      "misconceptions": [
        {{"text": "<common student mistake>", "mechanism": "<why they make it>"}}
      ],
      "socratic_ladder": [
        {{"question": "<easiest guiding question>", "hint": "<small hint>"}},
        {{"question": "<medium guiding question>", "hint": "<small hint>"}},
        {{"question": "<hardest guiding question>", "hint": "<small hint>"}}
      ],
      "key_terms": [
        {{"term": "<term>", "definition": "<simple Roman Urdu definition>"}}
      ]
    }}
  ]
}}

QUESTIONS TO PREPARE:

{questions_block}

Remember:
- Output ONE JSON object only.
- The "question_id" must be the exact integer from each QUESTION ID above.
- Roman Urdu for narrative content, English for scientific terms.
- Match the {tier_label} tier depth — do not write above or below that level.
"""
