"""Logs domain — request/response schemas."""
from __future__ import annotations

from datetime import datetime
from typing import Any, Optional
from uuid import UUID

from pydantic import BaseModel, field_validator

_LOG_LEVELS = ("debug", "info", "warn", "error", "fatal")
_DRAIN_TYPES = ("logtail", "datadog", "custom_webhook")
_RETENTION_BY_PLAN = {"free": 1, "starter": 7, "pro": 30, "growth": 90}


class LogConfigResponse(BaseModel):
    project_id: UUID
    debug_enabled: bool
    debug_expires_at: Optional[datetime] = None
    min_drain_level: str
    retention_days: int
    updated_at: Optional[datetime] = None

    model_config = {"from_attributes": True}


class UpdateLogConfigRequest(BaseModel):
    debug_enabled: Optional[bool] = None
    # How long to keep debug on (minutes). Capped at 120.
    debug_duration_minutes: Optional[int] = None
    min_drain_level: Optional[str] = None

    @field_validator("min_drain_level")
    @classmethod
    def validate_level(cls, v: Optional[str]) -> Optional[str]:
        if v is not None and v not in _LOG_LEVELS:
            raise ValueError(f"min_drain_level must be one of: {', '.join(_LOG_LEVELS)}")
        return v

    @field_validator("debug_duration_minutes")
    @classmethod
    def validate_duration(cls, v: Optional[int]) -> Optional[int]:
        if v is not None and v > 120:
            raise ValueError("debug_duration_minutes cannot exceed 120")
        return v


class LogDrainResponse(BaseModel):
    id: UUID
    project_id: UUID
    name: str
    destination_type: str
    endpoint_url: str
    min_level: str
    sources: Optional[list[str]] = None
    enabled: bool
    created_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}


class CreateLogDrainRequest(BaseModel):
    name: str
    destination_type: str
    endpoint_url: str
    auth_secret: Optional[str] = None
    min_level: str = "info"
    sources: Optional[list[str]] = None
    enabled: bool = True

    @field_validator("destination_type")
    @classmethod
    def validate_type(cls, v: str) -> str:
        if v not in _DRAIN_TYPES:
            raise ValueError(f"destination_type must be one of: {', '.join(_DRAIN_TYPES)}")
        return v

    @field_validator("min_level")
    @classmethod
    def validate_level(cls, v: str) -> str:
        if v not in _LOG_LEVELS:
            raise ValueError(f"min_level must be one of: {', '.join(_LOG_LEVELS)}")
        return v


class UpdateLogDrainRequest(BaseModel):
    endpoint_url: Optional[str] = None
    auth_secret: Optional[str] = None
    min_level: Optional[str] = None
    sources: Optional[list[str]] = None
    enabled: Optional[bool] = None

    @field_validator("min_level")
    @classmethod
    def validate_level(cls, v: Optional[str]) -> Optional[str]:
        if v is not None and v not in _LOG_LEVELS:
            raise ValueError(f"min_level must be one of: {', '.join(_LOG_LEVELS)}")
        return v


class TestDrainResponse(BaseModel):
    ok: bool
    status_code: Optional[int] = None
    error: Optional[str] = None
