"""Service models for registration and health tracking."""

from datetime import datetime
from typing import Any

from pydantic import BaseModel, ConfigDict, Field


class Service(BaseModel):
    """Registered service information."""

    id: str
    name: str
    description: str | None = None
    base_url: str
    health_url: str | None = None
    status: str = "active"
    permissions: list[str] = Field(default_factory=list)
    registered_at: datetime
    last_health_check: datetime | None = None
    metadata: dict[str, Any] = Field(default_factory=dict)

    model_config = ConfigDict(from_attributes=True)


class ServiceRegistration(BaseModel):
    """Service registration response."""

    id: str
    name: str
    api_key: str
    status: str
    registered_at: datetime

    model_config = ConfigDict(from_attributes=True)


class ServiceHealth(BaseModel):
    """Service health status."""

    service_id: str
    service_name: str
    status: str  # healthy, degraded, unhealthy
    checks: dict[str, Any] = Field(default_factory=dict)
    timestamp: datetime
    details: dict[str, Any] = Field(default_factory=dict)

    model_config = ConfigDict(from_attributes=True)


class HeartbeatResponse(BaseModel):
    """Service heartbeat response."""

    service_id: str
    acknowledged: bool
    last_heartbeat: datetime
    next_heartbeat_due: datetime | None = None

    model_config = ConfigDict(from_attributes=True)


class HealthStatus(BaseModel):
    """Overall health status check result."""

    status: str  # healthy, degraded, unhealthy
    timestamp: datetime
    services: dict[str, str] = Field(default_factory=dict)  # service_name -> status
    details: dict[str, Any] = Field(default_factory=dict)

    model_config = ConfigDict(from_attributes=True)
