from app.modules.agent.engine.orchestrator.actions.project_qa_actions import ProjectQaActions
from app.modules.agent.engine.orchestrator.execution_context import ExecutionContext
from app.modules.agent.engine.orchestrator.models import (
    ExecutionPlan,
    OutputContract,
    RoutingMeta,
    Scenario,
    TaskConstraints,
    TaskSpec,
)


def _ctx(message: str, rag_items: list[dict], files_map: dict[str, dict]) -> ExecutionContext:
    task = TaskSpec(
        task_id="task-1",
        dialog_session_id="dialog-1",
        rag_session_id="rag-1",
        user_message=message,
        scenario=Scenario.GENERAL_QA,
        routing=RoutingMeta(domain_id="project", process_id="qa", confidence=0.9, reason="test"),
        constraints=TaskConstraints(),
        output_contract=OutputContract(result_type="answer"),
        metadata={
            "rag_items": rag_items,
            "rag_context": "",
            "confluence_context": "",
            "files_map": files_map,
        },
    )
    plan = ExecutionPlan(
        plan_id="plan-1",
        task_id="task-1",
        scenario=Scenario.GENERAL_QA,
        template_id="tpl",
        template_version="1",
        steps=[],
    )
    return ExecutionContext(task=task, plan=plan, graph_resolver=lambda *_: None, graph_invoker=lambda *_: {})


def test_project_qa_actions_build_inventory_answer_from_code_sources() -> None:
    ctx = _ctx(
        "Какие каналы управления уже реализованы?",
        [],
        {
            "src/config_manager/v2/control/base.py": {"content": "class ControlChannel:\n    async def start(self):\n        ..."},
            "src/config_manager/v2/core/control_bridge.py": {
                "content": "class ControlChannelBridge:\n    async def on_start(self):\n        ...\n    async def on_status(self):\n        ..."
            },
            "src/config_manager/v2/control/http_channel.py": {
                "content": "class HttpControlChannel(ControlChannel):\n    async def start(self):\n        ...\n# http api"
            },
            "src/config_manager/v2/control/telegram_channel.py": {
                "content": "class TelegramControlChannel(ControlChannel):\n    async def start(self):\n        ...\n# telegram bot"
            },
        },
    )
    actions = ProjectQaActions()

    actions.classify_project_question(ctx)
    actions.collect_project_sources(ctx)
    actions.analyze_project_sources(ctx)
    actions.build_project_answer_brief(ctx)
    actions.compose_project_answer(ctx)

    answer = str(ctx.artifacts.get_content("final_answer", ""))
    assert "### Что реализовано" in answer
    assert "http channel" in answer.lower()
    assert "telegram channel" in answer.lower()
    assert "### Где смотреть в проекте" in answer

