from __future__ import annotations

from datetime import datetime
from typing import Optional

from pydantic import BaseModel, ConfigDict, Field


class ApiKeyCreateRequest(BaseModel):
    name: str
    description: Optional[str] = None


class ApiKeyToggleRequest(BaseModel):
    key_id: str = Field(..., alias="keyId")
    active: bool


class ApiKeyRevokeRequest(BaseModel):
    key_id: str = Field(..., alias="keyId")


class ApiKeyRecord(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    id: str
    name: str
    description: Optional[str] = None
    active: bool = True
    created_at: datetime = Field(..., alias="createdAt")
    last_used: Optional[datetime] = Field(None, alias="lastUsed")
    usage_count: int = Field(0, alias="usageCount")


class ApiKeyCreateResponse(BaseModel):
    key: str
    record: ApiKeyRecord
