"""Route and retrieval models for process v2."""

from __future__ import annotations

from dataclasses import dataclass, field


class V2Domain:
    DOCS = "DOCS"
    GENERAL = "GENERAL"


class V2Intent:
    DOC_EXPLAIN = "DOC_EXPLAIN"
    DOC_UPDATE = "DOC_UPDATE"
    GENERAL_QA = "GENERAL_QA"


class V2Subintent:
    SUMMARY = "SUMMARY"
    FIND_FILES = "FIND_FILES"
    API_EXPOSED = "API_EXPOSED"
    FROM_FEATURE = "FROM_FEATURE"


class V2AnchorType:
    GENERAL_OVERVIEW = "GENERAL_OVERVIEW"
    API_ENDPOINT = "API_ENDPOINT"
    ARCHITECTURE = "ARCHITECTURE"
    LOGIC_FLOW = "LOGIC_FLOW"
    DOMAIN_ENTITY = "DOMAIN_ENTITY"
    FIND_FILES = "FIND_FILES"


class V2ScopeType:
    """Grounded documentation scope (pre-LLM, catalog-backed)."""

    GLOBAL = "global"
    DOMAIN = "domain"
    SUBDOMAIN = "subdomain"
    ENTITY = "entity"
    UNKNOWN = "unknown"


@dataclass(slots=True)
class ScopeCandidate:
    """A single catalog-backed match candidate for intent-router scope grounding."""

    value: str
    score: float
    source_layer: str
    match_type: str


@dataclass(slots=True)
class V2RouteAnchors:
    """Якоря из запроса для retrieval и downstream."""

    entity_names: list[str] = field(default_factory=list)
    file_names: list[str] = field(default_factory=list)
    endpoint_paths: list[str] = field(default_factory=list)
    target_doc_hints: list[str] = field(default_factory=list)
    matched_aliases: list[str] = field(default_factory=list)
    process_domain: str | None = None
    process_subdomain: str | None = None
    candidate_domains: list[ScopeCandidate] = field(default_factory=list)
    candidate_subdomains: list[ScopeCandidate] = field(default_factory=list)
    candidate_entities: list[ScopeCandidate] = field(default_factory=list)
    candidate_apis: list[ScopeCandidate] = field(default_factory=list)


@dataclass(slots=True)
class V2RouteResult:
    routing_domain: str
    intent: str
    subintent: str
    user_query: str
    normalized_query: str
    target_terms: list[str] = field(default_factory=list)
    anchors: V2RouteAnchors = field(default_factory=V2RouteAnchors)
    confidence: float = 1.0
    routing_mode: str = "deterministic"
    llm_router_used: bool = False
    reason_short: str = ""
    scope_type: str = V2ScopeType.UNKNOWN

    @property
    def domain(self) -> str:
        """Совместимость с полем ``domain`` в логах и вызовах."""
        return self.routing_domain


@dataclass(slots=True)
class RetrievedSummary:
    path: str
    title: str
    summary: str
    document_id: str
    score: int
    confidence: float = 0.0
    match_reason: str = "semantic_match"
    is_primary: bool = False
    score_breakdown: dict[str, int] = field(default_factory=dict)


@dataclass(slots=True)
class RetrievedFile:
    path: str
    title: str
    document_id: str
    score: int
    confidence: float
    match_reason: str
    is_primary: bool = False
    score_breakdown: dict[str, int] = field(default_factory=dict)
