from __future__ import annotations

from typing import Any

from pydantic import BaseModel, Field


class IntentCandidate(BaseModel):
    intent_id: str
    score: float = 0.0
    reason: str | None = None
    model_name: str | None = None
    raw_label: str | None = None
    metadata: dict[str, Any] = Field(default_factory=dict)


class MatcherStageDebug(BaseModel):
    stage: str
    accepted: bool = False
    selected_intent: str | None = None
    score: float = 0.0
    elapsed_ms: float | None = None
    reason: str | None = None
    model_name: str | None = None
    backend: str | None = None
    fallback_used: bool = False
    raw_label: str | None = None
    error_message: str | None = None
    metadata: dict[str, Any] = Field(default_factory=dict)
    candidates: list[IntentCandidate] = Field(default_factory=list)


class RoutingDebug(BaseModel):
    selected_intent: str | None = None
    matched_stage: str | None = None
    decision: str = "reject"
    decision_reason: str | None = None
    confidence_grade: str | None = None
    total_match_latency_ms: float | None = None
    unknown_detected: bool = False
    extracted_slots: dict[str, Any] = Field(default_factory=dict)
    stages: list[MatcherStageDebug] = Field(default_factory=list)
