"""API Key schemas."""

from datetime import datetime
from typing import Optional, List
from uuid import UUID
from pydantic import BaseModel, Field, ConfigDict


class APIKeyBase(BaseModel):
    """Base API key schema."""

    name: Optional[str] = Field(None, max_length=255)
    scopes: List[str] = Field(default_factory=list)
    expires_days: Optional[int] = Field(None, ge=1, le=365)


class APIKeyCreate(APIKeyBase):
    """Schema for creating an API key."""

    pass


class APIKeyUpdate(BaseModel):
    """Schema for updating an API key."""

    name: Optional[str] = Field(None, max_length=255)


class APIKeyResponse(BaseModel):
    """Schema for API key response (without key_hash)."""

    model_config = ConfigDict(from_attributes=True)

    id: UUID
    name: Optional[str]
    key_prefix: str
    scopes: List[str]
    is_active: bool
    created_at: datetime
    expires_at: Optional[datetime] = None
    last_used_at: Optional[datetime] = None


class APIKeyCreateResponse(APIKeyResponse):
    """Schema for API key creation response (includes full key, ONLY ONCE!)."""

    key: str  # Full key shown only at creation


class APIKeyList(BaseModel):
    """Schema for list of API keys."""

    items: List[APIKeyResponse]
    total: int


class APIKeyValidation(BaseModel):
    """Schema for API key validation."""

    key: str
