"""Data models for the review pipeline."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any


@dataclass
class ReviewConfig:
    directory: str = ""  # Always set by CLI via config resolution
    project: str | None = None
    tag: str | None = None
    last_n: int | None = None
    model: str | None = None
    provider: str | None = None


@dataclass
class Distribution:
    min: float = 0.0
    max: float = 0.0
    mean: float = 0.0
    median: float = 0.0
    p95: float = 0.0
    p99: float = 0.0
    total: float = 0.0
    count: int = 0


@dataclass
class ModelBreakdown:
    model: str
    provider: str | None
    count: int = 0
    total_cost: float = 0.0
    avg_cost: float = 0.0
    avg_latency: float = 0.0
    total_tokens: int = 0
    has_cost: bool = False


@dataclass
class SpanKindBreakdown:
    kind: str
    count: int = 0
    total_latency: float = 0.0
    avg_latency: float = 0.0
    pct_of_total: float = 0.0
    error_count: int = 0


@dataclass
class AggregateStats:
    trace_count: int = 0
    span_count: int = 0
    cost: Distribution = field(default_factory=Distribution)
    latency: Distribution = field(default_factory=Distribution)
    tokens: Distribution = field(default_factory=Distribution)
    models: list[ModelBreakdown] = field(default_factory=list)
    span_kinds: list[SpanKindBreakdown] = field(default_factory=list)
    error_count: int = 0
    eval_count: int = 0  # traces with any grade (human or judge)
    human_eval_count: int = 0
    human_avg_grade: float | None = None
    judge_eval_count: int = 0
    judge_avg_grade: float | None = None


@dataclass
class Segment:
    key: str            # "tag:prod", "model:gpt-4o", "has_retrieval"
    dimension: str      # "tag", "model", "span_kind_presence"
    trace_count: int = 0
    trace_ids: list[str] = field(default_factory=list)
    stats: AggregateStats = field(default_factory=AggregateStats)


@dataclass
class SampledTrace:
    trace_id: str
    category: str  # "high_cost", "high_latency", "error", "random"
    cost: float = 0.0
    latency: float = 0.0
    tokens: int = 0
    spans_summary: list[dict[str, Any]] = field(default_factory=list)
    input_preview: str = ""
    output_preview: str = ""
    error: str | None = None


@dataclass
class Pattern:
    name: str
    severity: str  # "info", "warning", "critical"
    description: str
    evidence: list[str] = field(default_factory=list)


@dataclass
class ReviewResult:
    config: ReviewConfig
    stats: AggregateStats
    samples: list[SampledTrace] = field(default_factory=list)
    patterns: list[Pattern] = field(default_factory=list)
    llm_response: str | None = None
    mode: str = "bootstrap"  # "bootstrap" or "enhanced"
    command: str = "analyze"  # "basics" or "analyze"
