from __future__ import annotations

from app.core.agent.processes.v2.workflows.doc_explain_api_exposed.workflow_runtime.context import DocExplainApiExposedContext
from app.core.agent.utils.workflow import WorkflowStep


class FinalizeApiExposedAnswerStep(WorkflowStep[DocExplainApiExposedContext]):
    step_id = "finalize_api_exposed_answer"
    title = "Формирование ответа со списком API"

    async def run(self, context: DocExplainApiExposedContext) -> DocExplainApiExposedContext:
        if context.answer:
            return context
        if not context.endpoints:
            context.answer = "Не нашёл задокументированных API-эндпоинтов в выбранном scope."
            context.answer_generated_payload = {
                "answer_mode": "insufficient_evidence",
                "answer_length": len(context.answer),
            }
            return context
        context.answer = "\n".join(context.endpoints)
        context.answer_generated_payload = {
            "answer_mode": "deterministic",
            "answer_length": len(context.answer),
        }
        return context

    def trace_output(self, context: DocExplainApiExposedContext) -> dict[str, object]:
        return {"answer_length": len(context.answer)}

