from __future__ import annotations

from app.core.agent.processes.v2.workflows.doc_explain_find_files.workflow_runtime.buffered_graph import DocExplainFindFilesWorkflowGraph
from app.core.agent.processes.v2.workflows.doc_explain_find_files.steps.retrieval.candidate_rows import CandidateRowsBuilder
from app.core.agent.processes.v2.workflows.doc_explain_find_files.workflow_runtime.context import DocExplainFindFilesContext
from app.core.agent.processes.v2.workflows.doc_explain_find_files.steps.apply_files_evidence_gate_step import (
    ApplyFilesEvidenceGateStep,
)
from app.core.agent.processes.v2.workflows.doc_explain_find_files.steps.build_files_evidence_step import BuildFilesEvidenceStep
from app.core.agent.processes.v2.workflows.doc_explain_find_files.steps.fetch_rag_rows_step import FetchRagRowsStep
from app.core.agent.processes.v2.workflows.doc_explain_find_files.steps.finalize_find_files_answer_step import (
    FinalizeFindFilesAnswerStep,
)
from app.core.agent.processes.v2.workflows.doc_explain_find_files.steps.prepare_candidate_rows_step import (
    PrepareCandidateRowsStep,
)
from app.core.agent.processes.v2.workflows.doc_explain_find_files.steps.require_rag_session_step import RequireRagSessionStep
from app.core.agent.processes.v2.workflows.doc_explain_find_files.steps.resolve_retrieval_plan_step import (
    ResolveRetrievalPlanStep,
)
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 DocExplainFindFilesGraph(DocExplainFindFilesWorkflowGraph[DocExplainFindFilesContext]):
    def __init__(
        self,
        policy_resolver: RetrievalPlanResolver,
        rag_adapter: V2RagRetrievalAdapter,
        evidence_assembler: DocsEvidenceAssembler,
        evidence_gate: DocsEvidenceGate,
    ) -> None:
        super().__init__(
            workflow_id="v2.docs_explain.find_files",
            source="workflow.v2.find_files",
            steps=[
                RequireRagSessionStep(
                    missing_message="Для процесса v2 нужна активная RAG-сессия проекта с проиндексированной документацией."
                ),
                ResolveRetrievalPlanStep(policy_resolver),
                FetchRagRowsStep(rag_adapter),
                PrepareCandidateRowsStep(CandidateRowsBuilder()),
                BuildFilesEvidenceStep(evidence_assembler),
                ApplyFilesEvidenceGateStep(evidence_gate),
                FinalizeFindFilesAnswerStep(),
            ],
        )
