"""Dataclass models for every Chox API request and response."""

from __future__ import annotations

import dataclasses
from dataclasses import dataclass, field
from typing import Any, Literal, Optional


def _from_dict(cls: type, data: dict) -> Any:
    """Instantiate a dataclass from a dict, ignoring unknown keys."""
    names = {f.name for f in dataclasses.fields(cls)}
    return cls(**{k: v for k, v in data.items() if k in names})


# ---------------------------------------------------------------------------
# Evaluate
# ---------------------------------------------------------------------------


@dataclass
class EvaluateResponse:
    request_id: str
    verdict: Literal["allow", "block", "escalate", "warn"]
    action_type: Literal["read", "write", "delete", "financial", "unknown"]
    risk_score: float
    reason: str
    evaluated_at: str


# ---------------------------------------------------------------------------
# Projects
# ---------------------------------------------------------------------------


@dataclass
class Project:
    id: str
    name: str
    slug: str
    admin_key: str
    created_at: str
    updated_at: str


# ---------------------------------------------------------------------------
# Callers (AI systems)
# ---------------------------------------------------------------------------


@dataclass
class Caller:
    id: str
    project_id: str
    name: str
    token: str
    description: str
    active: bool
    created_at: str
    updated_at: str


# ---------------------------------------------------------------------------
# Integrations
# ---------------------------------------------------------------------------


@dataclass
class Integration:
    id: str
    project_id: str
    name: str
    integration_type: str
    destination_url: str
    active: bool
    created_at: str
    updated_at: str


# ---------------------------------------------------------------------------
# Logs
# ---------------------------------------------------------------------------


@dataclass
class RequestLog:
    id: str
    correlation_id: str
    project_id: str
    integration: str
    integration_type: str
    method: str
    destination_url: str
    request_path: str
    verdict: str
    created_at: str
    caller_id: Optional[str] = None
    caller_name: Optional[str] = None
    request_headers: dict = field(default_factory=dict)
    request_body: dict = field(default_factory=dict)
    response_status: Optional[int] = None
    response_headers: dict = field(default_factory=dict)
    response_body: dict = field(default_factory=dict)
    latency_ms: Optional[int] = None
    action_type: Optional[str] = None
    error_message: Optional[str] = None


# ---------------------------------------------------------------------------
# Stats
# ---------------------------------------------------------------------------


@dataclass
class ActionStats:
    action_type: str
    count: int


@dataclass
class HourlyStats:
    hour: str
    count: int


@dataclass
class TotalCount:
    count: int


# ---------------------------------------------------------------------------
# Config (public)
# ---------------------------------------------------------------------------


@dataclass
class GatewayConfig:
    gateway_url: str
