"""
prompts.py
All prompt templates for the Question Bank Generator.
Edit this file to change what gets sent to the model.
"""

# -----------------------------------------------------------------------
# SYSTEM PROMPT — sent with every request, tells the model how to behave
# -----------------------------------------------------------------------
BASE_SYSTEM_PROMPT = (
    "You are an expert question-paper setter. You generate high-quality, "
    "unambiguous exam questions. You ALWAYS respond with valid JSON only — "
    "no markdown fences, no commentary, no explanation outside the JSON. "
    "If you cannot follow the schema exactly, you still return your best-effort "
    "valid JSON matching it."
)

# -----------------------------------------------------------------------
# USER PROMPT TEMPLATES — one per question type
# {count}, {topic}, {difficulty} get filled in automatically
# -----------------------------------------------------------------------

MCQ_SINGLE_TEMPLATE = """Generate {count} multiple-choice questions (single correct answer)
on the topic "{topic}" at {difficulty} difficulty.

Return a JSON array where each item has exactly this shape:
{{
  "question_text": "string",
  "options": ["option A", "option B", "option C", "option D"],
  "correct_answer": ["option B"],
  "explanation": "why this option is correct"
}}
Return ONLY the JSON array."""

MCQ_MULTI_TEMPLATE = """Generate {count} multiple-choice questions (one OR MORE correct answers)
on the topic "{topic}" at {difficulty} difficulty.

Return a JSON array where each item has exactly this shape:
{{
  "question_text": "string",
  "options": ["option A", "option B", "option C", "option D"],
  "correct_answer": ["option A", "option C"],
  "explanation": "why these options are correct and others are not"
}}
Return ONLY the JSON array."""

TRUE_FALSE_TEMPLATE = """Generate {count} True/False questions on the topic "{topic}"
at {difficulty} difficulty.

Return a JSON array where each item has exactly this shape:
{{
  "question_text": "string (a statement to be judged true or false)",
  "options": ["True", "False"],
  "correct_answer": ["True"],
  "explanation": "why the statement is true or false"
}}
Return ONLY the JSON array."""

FILL_BLANK_TEMPLATE = """Generate {count} fill-in-the-blank questions on the topic "{topic}"
at {difficulty} difficulty. Use "_____" to mark the blank in the question text.

Return a JSON array where each item has exactly this shape:
{{
  "question_text": "string with a _____ blank",
  "options": [],
  "correct_answer": ["the exact word or phrase that fills the blank"],
  "explanation": "brief reasoning"
}}
Return ONLY the JSON array."""

SHORT_ANSWER_TEMPLATE = """Generate {count} short-answer questions (answerable in 1-3 sentences)
on the topic "{topic}" at {difficulty} difficulty.

Return a JSON array where each item has exactly this shape:
{{
  "question_text": "string",
  "options": [],
  "correct_answer": ["a concise model answer"],
  "explanation": "grading notes / key points to look for"
}}
Return ONLY the JSON array."""

LONG_ANSWER_TEMPLATE = """Generate {count} long-answer / descriptive questions
on the topic "{topic}" at {difficulty} difficulty.

Return a JSON array where each item has exactly this shape:
{{
  "question_text": "string",
  "options": [],
  "correct_answer": ["a model answer outline, 4-8 sentences"],
  "explanation": "key points a grader should check for"
}}
Return ONLY the JSON array."""

MATCH_FOLLOWING_TEMPLATE = """Generate {count} "match the following" questions on the topic "{topic}"
at {difficulty} difficulty. Each question should have 4 items in the left column
and 4 items in the right column (right column may include one extra distractor).

Return a JSON array where each item has exactly this shape:
{{
  "question_text": "Match the items in Column A with Column B",
  "options": {{
    "left": ["A1", "A2", "A3", "A4"],
    "right": ["B1", "B2", "B3", "B4"]
  }},
  "correct_answer": {{"A1": "B3", "A2": "B1", "A3": "B4", "A4": "B2"}},
  "explanation": "brief reasoning for the mapping"
}}
Return ONLY the JSON array."""

ASSERTION_REASON_TEMPLATE = """Generate {count} Assertion-Reason questions on the topic "{topic}"
at {difficulty} difficulty. Each question gives an Assertion (A) and a Reason (R).

Return a JSON array where each item has exactly this shape:
{{
  "question_text": "Assertion (A): ...\\nReason (R): ...",
  "options": [
    "Both A and R are true, and R is the correct explanation of A",
    "Both A and R are true, but R is NOT the correct explanation of A",
    "A is true but R is false",
    "A is false but R is true"
  ],
  "correct_answer": ["Both A and R are true, and R is the correct explanation of A"],
  "explanation": "why this option is correct"
}}
Return ONLY the JSON array."""

# -----------------------------------------------------------------------
# LOOKUP TABLE — maps question_type string -> its template
# -----------------------------------------------------------------------
PROMPT_TEMPLATES = {
    "mcq_single": MCQ_SINGLE_TEMPLATE,
    "mcq_multi": MCQ_MULTI_TEMPLATE,
    "true_false": TRUE_FALSE_TEMPLATE,
    "fill_blank": FILL_BLANK_TEMPLATE,
    "short_answer": SHORT_ANSWER_TEMPLATE,
    "long_answer": LONG_ANSWER_TEMPLATE,
    "match_following": MATCH_FOLLOWING_TEMPLATE,
    "assertion_reason": ASSERTION_REASON_TEMPLATE,
}


def build_user_prompt(question_type: str, topic: str, count: int, difficulty: str) -> str:
    """Fills a template with the actual topic/count/difficulty and returns the final prompt string."""
    template = PROMPT_TEMPLATES.get(question_type)
    if template is None:
        raise ValueError(f"Unsupported question_type: {question_type}")
    return template.format(count=count, topic=topic, difficulty=difficulty)