from __future__ import annotations

from app.core.agent.processes.v2.workflows.doc_explain_summary.workflow_runtime.buffered_graph import DocExplainSummaryWorkflowGraph
from app.core.agent.processes.v2.workflows.doc_explain_summary.steps.retrieval.candidate_rows import CandidateRowsBuilder
from app.core.agent.processes.v2.workflows.doc_explain_summary.workflow_runtime.context import DocExplainSummaryContext
from app.core.agent.processes.v2.workflows.doc_explain_summary.steps.apply_summary_evidence_gate_step import (
    ApplySummaryEvidenceGateStep,
)
from app.core.agent.processes.v2.workflows.doc_explain_summary.steps.build_summary_evidence_step import BuildSummaryEvidenceStep
from app.core.agent.processes.v2.workflows.doc_explain_summary.steps.fetch_rag_rows_step import FetchRagRowsStep
from app.core.agent.processes.v2.workflows.doc_explain_summary.steps.generate_summary_answer_step import (
    GenerateSummaryAnswerStep,
)
from app.core.agent.processes.v2.workflows.doc_explain_summary.steps.prepare_candidate_rows_step import (
    PrepareCandidateRowsStep,
)
from app.core.agent.processes.v2.workflows.doc_explain_summary.steps.require_rag_session_step import RequireRagSessionStep
from app.core.agent.processes.v2.workflows.doc_explain_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
from app.core.agent.utils.process_v2.plan_resolver import RetrievalPlanResolver
from app.core.agent.utils.process_v2.rag_retrieval import V2RagRetrievalAdapter


class DocExplainSummaryGraph(DocExplainSummaryWorkflowGraph[DocExplainSummaryContext]):
    def __init__(
        self,
        llm: AgentLlmService,
        policy_resolver: RetrievalPlanResolver,
        rag_adapter: V2RagRetrievalAdapter,
        evidence_assembler: DocsEvidenceAssembler,
        evidence_gate: DocsEvidenceGate,
    ) -> None:
        super().__init__(
            workflow_id="v2.docs_explain.summary",
            source="workflow.v2.summary",
            steps=[
                RequireRagSessionStep(
                    missing_message="Для процесса v2 нужна активная RAG-сессия проекта с проиндексированной документацией."
                ),
                ResolveRetrievalPlanStep(policy_resolver),
                FetchRagRowsStep(rag_adapter),
                PrepareCandidateRowsStep(CandidateRowsBuilder()),
                BuildSummaryEvidenceStep(evidence_assembler),
                ApplySummaryEvidenceGateStep(evidence_gate),
                GenerateSummaryAnswerStep(llm),
            ],
        )
