from __future__ import annotations

from app.core.agent.processes.v2.workflows.general_qa_summary.workflow_runtime.buffered_graph import GeneralQaSummaryWorkflowGraph
from app.core.agent.processes.v2.workflows.general_qa_summary.steps.retrieval.candidate_rows import CandidateRowsBuilder
from app.core.agent.processes.v2.workflows.general_qa_summary.workflow_runtime.context import GeneralQaSummaryContext
from app.core.agent.processes.v2.workflows.general_qa_summary.steps.apply_summary_evidence_gate_step import (
    ApplySummaryEvidenceGateStep,
)
from app.core.agent.processes.v2.workflows.general_qa_summary.steps.build_summary_evidence_step import BuildSummaryEvidenceStep
from app.core.agent.processes.v2.workflows.general_qa_summary.steps.fetch_rag_rows_step import FetchRagRowsStep
from app.core.agent.processes.v2.workflows.general_qa_summary.steps.generate_general_summary_answer_step import (
    GenerateGeneralSummaryAnswerStep,
)
from app.core.agent.processes.v2.workflows.general_qa_summary.steps.prepare_candidate_rows_step import (
    PrepareCandidateRowsStep,
)
from app.core.agent.processes.v2.workflows.general_qa_summary.steps.require_rag_session_step import RequireRagSessionStep
from app.core.agent.processes.v2.workflows.general_qa_summary.steps.resolve_retrieval_plan_step import (
    ResolveRetrievalPlanStep,
)
from app.core.agent.utils.llm import AgentLlmService
from app.core.agent.utils.process_v2.evidence.assembler import DocsEvidenceAssembler
from app.core.agent.utils.process_v2.evidence.gate import DocsEvidenceGate, EvidenceGateDecision
from app.core.agent.utils.process_v2.plan_resolver import RetrievalPlanResolver
from app.core.agent.utils.process_v2.rag_retrieval import V2RagRetrievalAdapter


class GeneralQaSummaryGraph(GeneralQaSummaryWorkflowGraph[GeneralQaSummaryContext]):
    def __init__(
        self,
        llm: AgentLlmService,
        policy_resolver: RetrievalPlanResolver,
        rag_adapter: V2RagRetrievalAdapter,
        evidence_assembler: DocsEvidenceAssembler,
        evidence_gate: DocsEvidenceGate,
    ) -> None:
        super().__init__(
            workflow_id="v2.general_qa.summary",
            source="workflow.v2.general_summary",
            steps=[
                RequireRagSessionStep(
                    missing_message="Не могу собрать grounded summary без активной RAG-сессии с проиндексированной документацией.",
                    missing_gate=EvidenceGateDecision(
                        passed=False,
                        answer_mode="insufficient_evidence",
                        reason="missing_rag_session",
                        message="Не могу собрать grounded summary без активной RAG-сессии с проиндексированной документацией.",
                    ),
                ),
                ResolveRetrievalPlanStep(policy_resolver),
                FetchRagRowsStep(rag_adapter),
                PrepareCandidateRowsStep(CandidateRowsBuilder()),
                BuildSummaryEvidenceStep(evidence_assembler),
                ApplySummaryEvidenceGateStep(evidence_gate),
                GenerateGeneralSummaryAnswerStep(llm),
            ],
        )
