"""
Pydantic models for FSAL API requests and responses.

Defines the data structures for all file system operations.
These schemas maintain 1:1 compatibility with the existing API contract.
"""

from __future__ import annotations

from pydantic import BaseModel, Field


class PathIn(BaseModel):
    """Base model for operations requiring a single path."""

    path: str = Field(..., description="Path relative to FSAL_ROOT")


class ListIn(PathIn):
    """Request model for directory listing operations."""

    glob: str | None = None
    files_only: bool = False
    dirs_only: bool = False
    max_entries: int = 5000
    recursive: bool = Field(
        default=False,
        description="If true, recursively list all entries in subdirectories",
    )
    max_depth: int | None = Field(
        default=None,
        description="Maximum recursion depth (None = unlimited, requires recursive=True)",
    )


class ReadIn(PathIn):
    """Request model for file read operations."""

    offset: int | None = None
    length: int | None = None
    as_text: bool = False
    encoding: str = "utf-8"


class WriteIn(PathIn):
    """Request model for file write operations."""

    content_b64: str = Field(
        ...,
        max_length=134_217_728,  # 128MB base64 = ~96MB decoded
        description="Base64-encoded file content (max 128MB)",
    )
    mode: int | None = 0o644
    make_dirs: bool = False


class MoveCopyIn(BaseModel):
    """Request model for move/copy operations."""

    src: str = Field(..., description="Source path relative to FSAL_ROOT")
    dst: str = Field(..., description="Destination path relative to FSAL_ROOT")
    overwrite: bool = False


class MkdirIn(PathIn):
    """Request model for directory creation."""

    exist_ok: bool = True
    mode: int | None = 0o755


class DeleteIn(PathIn):
    """Request model for deletion operations."""

    recursive: bool = False


class HashIn(PathIn):
    """Request model for file hashing operations."""

    algo: str = "sha256"


class ExistsIn(PathIn):
    """Request model for file/directory existence check."""

    pass


class StatIn(PathIn):
    """Request model for file/directory stat operation."""

    pass


class Entry(BaseModel):
    """Model representing a file system entry."""

    path: str = Field(..., description="Relative path from FSAL_ROOT")
    is_dir: bool = Field(..., description="True if entry is a directory")
    size: int = Field(..., description="Size in bytes (0 for directories)")
    mtime: float = Field(..., description="Last modification time as Unix timestamp")


class StatResult(BaseModel):
    """Model representing detailed file/directory statistics."""

    path: str = Field(..., description="Relative path from FSAL_ROOT")
    exists: bool = Field(..., description="Whether the path exists")
    is_dir: bool = Field(default=False, description="True if entry is a directory")
    is_file: bool = Field(default=False, description="True if entry is a regular file")
    size: int = Field(default=0, description="Size in bytes")
    mtime: float | None = Field(default=None, description="Last modification time")
    ctime: float | None = Field(default=None, description="Creation/change time")
    mode: int | None = Field(default=None, description="File permissions")
