from __future__ import annotations

from pathlib import Path

from tests.pipeline_setup.suite_02_pipeline.pipeline_intent_rag.helpers.llm_prompt_loader import LlmPromptCatalogLoader

_PROMPT_CATALOG = Path(__file__).resolve().parents[3] / "pipeline_setup_v2" / "llm_prompts.yaml"


def test_prompt_catalog_loads_intent_templates() -> None:
    loader = LlmPromptCatalogLoader()
    catalog = loader.load(_PROMPT_CATALOG)

    code_prompt = loader.select_for_intent(catalog, "CODE_QA")

    assert code_prompt["template_id"] == "intent_code_qa_v5"
    assert "senior Python-инженер" in code_prompt["system_prompt"]
    assert "естественным инженерным языком" in code_prompt["task_prompt"]


def test_prompt_catalog_prefers_sub_intent_template() -> None:
    loader = LlmPromptCatalogLoader()
    catalog = loader.load(_PROMPT_CATALOG)

    prompt = loader.select(catalog, intent="CODE_QA", sub_intent="FIND_TESTS")

    assert prompt["template_id"] == "intent_code_qa_find_tests_ru_v4"
    assert "тестовое покрытие" in prompt["system_prompt"]
    assert "прямых тестов нет" in prompt["task_prompt"]


def test_prompt_catalog_falls_back_to_intent_template_for_unknown_sub_intent() -> None:
    loader = LlmPromptCatalogLoader()
    catalog = loader.load(_PROMPT_CATALOG)

    prompt = loader.select(catalog, intent="CODE_QA", sub_intent="UNKNOWN_SUB_INTENT")

    assert prompt["template_id"] == "intent_code_qa_v5"
    assert "естественным инженерным языком" in prompt["task_prompt"]


def test_prompt_catalog_falls_back_to_default_for_unknown_intent() -> None:
    loader = LlmPromptCatalogLoader()
    catalog = loader.load(_PROMPT_CATALOG)

    prompt = loader.select_for_intent(catalog, "UNKNOWN_INTENT")

    assert prompt["template_id"] == "intent_default_v2"
    assert "технический ассистент" in prompt["system_prompt"]
