"""Scanner prompts for different question types.

Each question type has its own prompt file with SYSTEM_PROMPT and USER_TEMPLATE.
Common TikZ guidelines are in common.py and can be imported into individual prompts.

Subject-specific prompts are generated by injecting subject config into templates.
"""

from vbagent.prompts.scanner.mcq_sc import SYSTEM_PROMPT as MCQ_SC_PROMPT
from vbagent.prompts.scanner.mcq_mc import SYSTEM_PROMPT as MCQ_MC_PROMPT
from vbagent.prompts.scanner.subjective import SYSTEM_PROMPT as SUBJECTIVE_PROMPT
from vbagent.prompts.scanner.assertion_reason import SYSTEM_PROMPT as ASSERTION_REASON_PROMPT
from vbagent.prompts.scanner.passage import SYSTEM_PROMPT as PASSAGE_PROMPT
from vbagent.prompts.scanner.match import SYSTEM_PROMPT as MATCH_PROMPT
from vbagent.prompts.scanner.common import (
    TIKZ_GUIDELINES,
    TIKZ_GUIDELINES_SHORT,
    LATEX_FORMATTING_RULES,
    PGFPLOTS_EXAMPLE,
    OPTIONS_WITH_DIAGRAMS,
    DIAGRAM_PLACEHOLDER,
)

# Mapping from question type to prompt
SCANNER_PROMPTS = {
    "mcq_sc": MCQ_SC_PROMPT,
    "mcq_mc": MCQ_MC_PROMPT,
    "subjective": SUBJECTIVE_PROMPT,
    "assertion_reason": ASSERTION_REASON_PROMPT,
    "passage": PASSAGE_PROMPT,
    "match": MATCH_PROMPT,
}

# Default user template for all scanner types
USER_TEMPLATE = "Extract LaTeX from this {subject} question image."


def _get_subject_section(subject: str) -> str:
    """Generate subject-specific prompt section.
    
    Args:
        subject: Subject name (physics, chemistry, etc.)
        
    Returns:
        Subject-specific instructions to append to prompt
    """
    from vbagent.prompts.subjects import get_subject_config, SUBJECTS
    
    if subject not in SUBJECTS:
        subject = "physics"  # Default fallback
    
    config = get_subject_config(subject)
    
    section = f"""
---

## Subject: {config.display_name}

You are an {config.expert_role}.

{config.package_instructions}

{config.formatting_rules}

{config.diagram_instructions}

**Example {config.display_name} Problem:**
{config.example_problem}

{config.example_solution}
"""
    return section


def get_scanner_prompt(question_type: str, subject: str = "physics") -> str:
    """Get the scanner prompt for a given question type and subject.
    
    Args:
        question_type: The type of question (mcq_sc, mcq_mc, etc.)
        subject: The subject (physics, chemistry, mathematics, biology)
        
    Returns:
        The system prompt for that question type with subject-specific additions
        
    Raises:
        KeyError: If question_type is not recognized
    """
    if question_type not in SCANNER_PROMPTS:
        # Fall back to mcq_sc for unknown types
        base_prompt = SCANNER_PROMPTS["mcq_sc"]
    else:
        base_prompt = SCANNER_PROMPTS[question_type]
    
    # Replace "physics" with subject in base prompt
    if subject != "physics":
        base_prompt = base_prompt.replace("physics question", f"{subject} question")
        base_prompt = base_prompt.replace("physics problem", f"{subject} problem")
        base_prompt = base_prompt.replace("expert physicist", f"expert {subject} specialist")
    
    # Add subject-specific section
    subject_section = _get_subject_section(subject)
    
    return base_prompt + subject_section


def get_user_template(subject: str = "physics") -> str:
    """Get user template with subject.
    
    Args:
        subject: The subject name
        
    Returns:
        User template string
    """
    return USER_TEMPLATE.format(subject=subject)


__all__ = [
    "SCANNER_PROMPTS",
    "USER_TEMPLATE",
    "get_scanner_prompt",
    "get_user_template",
    "MCQ_SC_PROMPT",
    "MCQ_MC_PROMPT",
    "SUBJECTIVE_PROMPT",
    "ASSERTION_REASON_PROMPT",
    "PASSAGE_PROMPT",
    "MATCH_PROMPT",
    # Common prompt components
    "TIKZ_GUIDELINES",
    "TIKZ_GUIDELINES_SHORT",
    "LATEX_FORMATTING_RULES",
    "PGFPLOTS_EXAMPLE",
    "OPTIONS_WITH_DIAGRAMS",
    "DIAGRAM_PLACEHOLDER",
]
