"""Модели состояния и результата runtime-оркестратора."""

from __future__ import annotations

from typing import Any

from pydantic import BaseModel, ConfigDict, Field

from app.modules.agent.runtime.steps.context.contracts import (
    AnswerSynthesisInput,
    DiagnosticsReport,
    EvidenceBundle,
    RetrievalRequest,
    RetrievalResult,
)
from app.modules.agent.runtime.steps.gates.post.post_gate import RuntimeValidationResult
from app.modules.agent.intent_router_v2.models import ConversationState, IntentRouterResult, RepoContext


class RuntimeDraftAnswer(BaseModel):
    model_config = ConfigDict(extra="forbid")

    prompt_name: str
    prompt_payload: str
    answer: str = ""


class RuntimeFinalResult(BaseModel):
    model_config = ConfigDict(extra="forbid")

    final_answer: str
    answer_mode: str = "normal"
    repair_used: bool = False
    llm_used: bool = False
    draft_answer: RuntimeDraftAnswer | None = None
    validation: RuntimeValidationResult = Field(default_factory=RuntimeValidationResult)
    router_result: IntentRouterResult | None = None
    retrieval_request: RetrievalRequest | None = None
    retrieval_result: RetrievalResult | None = None
    evidence_pack: EvidenceBundle | None = None
    diagnostics: DiagnosticsReport
    runtime_trace: list[dict[str, Any]] = Field(default_factory=list)


class RuntimeExecutionState(BaseModel):
    model_config = ConfigDict(extra="forbid")

    user_query: str
    rag_session_id: str
    conversation_state: ConversationState = Field(default_factory=ConversationState)
    repo_context: RepoContext = Field(default_factory=RepoContext)
    router_result: IntentRouterResult | None = None
    retrieval_request: RetrievalRequest | None = None
    retrieval_result: RetrievalResult | None = None
    evidence_pack: EvidenceBundle | None = None
    synthesis_input: AnswerSynthesisInput | None = None
    diagnostics: DiagnosticsReport | None = None
    answer_mode: str = "normal"
    degraded_message: str = ""
    final_result: RuntimeFinalResult | None = None
