from app.modules.agent.engine.orchestrator.models import (
    ArtifactType,
    OutputContract,
    OutputSection,
    RoutingMeta,
    Scenario,
    TaskConstraints,
    TaskSpec,
)
from app.modules.agent.engine.orchestrator.quality_metrics import QualityMetricsCalculator
from app.modules.agent.engine.orchestrator.template_registry import ScenarioTemplateRegistry
from app.modules.agent.engine.orchestrator.execution_context import ExecutionContext
from app.modules.agent.engine.orchestrator.models import PlanStatus


def test_quality_metrics_coverage_reflects_missing_required_sections() -> None:
    task = TaskSpec(
        task_id="quality-2",
        dialog_session_id="dialog-1",
        rag_session_id="rag-1",
        mode="auto",
        user_message="Explain architecture",
        scenario=Scenario.EXPLAIN_PART,
        routing=RoutingMeta(domain_id="project", process_id="qa", confidence=0.9, reason="test"),
        constraints=TaskConstraints(allow_writes=False),
        output_contract=OutputContract(
            result_type="answer",
            sections=[
                OutputSection(name="sequence_diagram", format="mermaid"),
                OutputSection(name="use_cases", format="markdown"),
                OutputSection(name="summary", format="markdown"),
            ],
        ),
        metadata={"rag_context": "A", "confluence_context": "", "files_map": {}},
    )

    plan = ScenarioTemplateRegistry().build(task)
    plan.status = PlanStatus.COMPLETED
    ctx = ExecutionContext(
        task=task,
        plan=plan,
        graph_resolver=lambda _d, _p: object(),
        graph_invoker=lambda _g, _s, _id: {},
    )
    ctx.artifacts.put(key="final_answer", artifact_type=ArtifactType.TEXT, content="Only summary text")

    metrics = QualityMetricsCalculator().build(ctx, step_results=[])

    assert metrics["coverage"]["score"] < 1.0
    assert "sequence_diagram" in metrics["coverage"]["missing_items"]
