import uuid

from sqlalchemy import BigInteger, Boolean, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base, PGUUID, TenantScopedMixin, TimestampMixin, UUIDPKMixin


class UploadedFile(UUIDPKMixin, TenantScopedMixin, TimestampMixin, Base):
    """Metadata for a file uploaded via POST /uploads and saved to local
    disk under backend/uploads/{tenant_id}/ (see app/routers/uploads.py).
    Referenced from a Master/Form "file" field's value by this row's id —
    the field_values JSONB never holds the file itself, matching the
    "reference" field type's pattern of storing another table's id."""

    __tablename__ = "uploaded_files"

    filename: Mapped[str] = mapped_column(String(255), nullable=False)
    content_type: Mapped[str] = mapped_column(String(127), nullable=False)
    size_bytes: Mapped[int] = mapped_column(BigInteger, nullable=False)
    storage_path: Mapped[str] = mapped_column(String(500), nullable=False)
    # A public website asset (POST /pages/assets) — served to anyone at
    # /site/_files/<tenant>/<id>/<name> (app/routers/public_site.py).
    is_public: Mapped[bool] = mapped_column(Boolean, nullable=False, server_default="false")
    # Uploaded by a signed-out visitor to this public form (app/routers/public_forms.py).
    public_form_id: Mapped[uuid.UUID | None] = mapped_column(PGUUID(as_uuid=True))
    created_by: Mapped[uuid.UUID | None] = mapped_column(PGUUID(as_uuid=True), ForeignKey("users.id"))
