"""HTTP schemas for the file management API."""

from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field, field_serializer

from agenteval.storage.db import FileCategoryDB, FileRecordDB, iso_utc


class CategoryCreate(BaseModel):
    name: str
    parent_id: str | None = None


class CategoryUpdate(BaseModel):
    name: str


class FileCategoryResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: str
    name: str
    parent_id: str | None
    children: list["FileCategoryResponse"] = Field(default_factory=list)

    @classmethod
    def from_record(cls, category: FileCategoryDB) -> "FileCategoryResponse":
        return cls(
            id=category.id or "",
            name=category.name,
            parent_id=category.parent_id,
            children=[],
        )


class FileRecordResponse(BaseModel):
    id: str
    original_name: str
    file_size: int
    mime_type: str
    file_ext: str
    category_id: str | None
    storage_directory: str
    created_at: datetime | None

    @field_serializer("created_at")
    def serialize_created_at(self, value: datetime | None) -> str | None:
        return iso_utc(value)

    @classmethod
    def from_record(cls, record: FileRecordDB) -> "FileRecordResponse":
        return cls(
            id=record.id or "",
            original_name=record.original_name,
            file_size=record.file_size,
            mime_type=record.mime_type,
            file_ext=record.file_ext,
            category_id=record.category_id,
            storage_directory="data/files",
            created_at=record.created_at,
        )


class FileUploadConfigResponse(BaseModel):
    allowed_extensions: list[str]
    max_upload_size_mb: int


class OkResponse(BaseModel):
    ok: bool = True
