from __future__ import annotations

from tests.pipeline_setup.suite_02_pipeline.pipeline_intent_rag.helpers.prompt_payload_builder import PromptPayloadBuilder


def test_build_prompt_payload_includes_layer_guide_and_explain_quotas() -> None:
    builder = PromptPayloadBuilder()
    rag_rows = [
        {"layer": "C1_SYMBOL_CATALOG", "path": "src/context.py", "title": "Context", "content": "class Context"},
        {"layer": "C1_SYMBOL_CATALOG", "path": "src/other.py", "title": "Other", "content": "class Other"},
        {"layer": "C0_SOURCE_CHUNKS", "path": "src/context.py", "title": "Context chunk 1", "content": "def start(self): pass"},
        {"layer": "C0_SOURCE_CHUNKS", "path": "src/context.py", "title": "Context chunk 2", "content": "def stop(self): pass"},
        {"layer": "C0_SOURCE_CHUNKS", "path": "src/context.py", "title": "Context chunk 3", "content": "def status(self): pass"},
        {"layer": "C2_DEPENDENCY_GRAPH", "path": "src/context.py", "title": "Context->Worker", "content": "Context calls Worker"},
        {"layer": "C4_SEMANTIC_ROLES", "path": "src/context.py", "title": "Context", "content": "role: pipeline_stage"},
        {"layer": "C4_SEMANTIC_ROLES", "path": "src/control.py", "title": "ControlChannel", "content": "role: model"},
        {"layer": "C3_ENTRYPOINTS", "path": "src/main.py", "title": "main", "content": "entrypoint"},
    ]

    payload = builder.build(
        "Объясни как работает Context",
        rag_rows,
        prompt_template={"template_id": "t", "system_prompt": "sys", "task_prompt": "task"},
        sub_intent="EXPLAIN",
        default_system_prompt="sys-default",
        default_task_prompt="task-default",
        default_template_id="default",
        system_prompt_version="v1",
    )
    user_prompt = payload["user_prompt"]

    assert "Как интерпретировать слои RAG" in user_prompt
    assert user_prompt.count("[C1_SYMBOL_CATALOG]") == 1
    assert user_prompt.count("[C0_SOURCE_CHUNKS]") == 2
    assert user_prompt.count("[C2_DEPENDENCY_GRAPH]") == 1
    assert user_prompt.count("[C4_SEMANTIC_ROLES]") == 1
    assert user_prompt.count("[C3_ENTRYPOINTS]") == 1


def test_find_entrypoints_prompt_prefers_http_route_rows() -> None:
    builder = PromptPayloadBuilder()
    rag_rows = [
        {
            "layer": "C3_ENTRYPOINTS",
            "path": "src/app.py",
            "title": "candidate",
            "content": "lifecycle start",
            "metadata": {"entry_type": "startup"},
        },
        {
            "layer": "C3_ENTRYPOINTS",
            "path": "src/http_app.py",
            "title": "GET /health declared in HttpControlAppFactory",
            "content": "raw",
            "metadata": {
                "http_method": "GET",
                "route_path": "/health",
                "declaring_symbol": "HttpControlAppFactory",
                "handler_symbol": "HttpControlAppFactory.create.health",
                "decorator_text": "@app.get('/health')",
                "summary_text": "GET /health declared in HttpControlAppFactory",
            },
        },
    ]

    payload = builder.build(
        "Где health endpoint?",
        rag_rows,
        prompt_template={"template_id": "t", "system_prompt": "sys", "task_prompt": "task"},
        sub_intent="FIND_ENTRYPOINTS",
        default_system_prompt="sys-default",
        default_task_prompt="task-default",
        default_template_id="default",
        system_prompt_version="v1",
    )
    user_prompt = payload["user_prompt"]

    assert "GET /health declared in HttpControlAppFactory" in user_prompt
    assert "Declared in: HttpControlAppFactory" in user_prompt
    assert "Handler: HttpControlAppFactory.create.health" in user_prompt
