from __future__ import annotations

from pathlib import Path
from typing import List

from analogai.deepthink.messaging.question_answering.enums.synthesized_text_type import (
    SynthesizedTextType,
)

class QASynthesizerPromptComposer:
    def build_system_prompt(self) -> str:
        return ""

    def build_user_message(
        self,
        question: str,
        entities: List[str],
        belief_block: str,
    ) -> str:
        entities_text = ", ".join(entities) if entities else "(none detected)"
        belief_text = belief_block or "(no matching beliefs found)"
        return (
            f"Question: {question}\n"
            f"Entities: {entities_text}\n"
            "Belief context:\n"
            f"{belief_text}\n\n"
            "Answer:"
        )

    def build_synthesis_prompt(
        self,
        question: str,
        tasks: str,
        facts: str,
        qa_history: str,
        synthesized_text_type: SynthesizedTextType,
    ) -> str:
        template = _load_synthesizer_prompt_template(synthesized_text_type)
        return template.format(
            question=question,
            tasks=tasks,
            facts=facts,
            qa_history=qa_history,
        )


def _load_synthesizer_prompt_template(synthesized_text_type: SynthesizedTextType) -> str:
    prompts_dir = Path(__file__).resolve().parents[2] / "infrastructure" / "prompts"
    filename_map = {
        SynthesizedTextType.EXACT_MATCH: "qa_synthesizer_prompt_exact_match.txt",
        SynthesizedTextType.DETAILED: "qa_synthesizer_prompt_detailed.txt",
    }
    prompt_path = prompts_dir / filename_map[synthesized_text_type]
    if not prompt_path.exists():
        raise FileNotFoundError(f"Prompt file not found: {prompt_path}")
    return prompt_path.read_text(encoding="utf-8")
