"""Client materials attached to a project — notes, uploaded files, links.

The intake side of the direction loop: everything an admin gathers from the
client lands here, and the briefing export is assembled from these rows. Files
live on disk under the data root (D13); the row stores only a relative path and
the original filename as metadata.
"""

from datetime import datetime

from sqlalchemy import DateTime, ForeignKey, Integer, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base


class Material(Base):
    __tablename__ = "materials"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    project_id: Mapped[int] = mapped_column(
        Integer, ForeignKey("projects.id", ondelete="CASCADE"), index=True
    )

    # note | file | link (MaterialKind). Decides which of the fields below carry
    # the content; the others stay empty.
    kind: Mapped[str] = mapped_column(String(16))

    title: Mapped[str] = mapped_column(String(200), default="", server_default="")
    body: Mapped[str] = mapped_column(Text, default="", server_default="")  # notes
    url: Mapped[str] = mapped_column(String(500), default="", server_default="")  # links

    # Files: stored under a UUID name relative to the data root; the original
    # filename is metadata only and never used as a path.
    file_path: Mapped[str] = mapped_column(String(300), default="", server_default="")
    file_name: Mapped[str] = mapped_column(String(255), default="", server_default="")
    mime: Mapped[str] = mapped_column(String(120), default="", server_default="")
    size: Mapped[int] = mapped_column(Integer, default=0, server_default="0")

    added_by: Mapped[str] = mapped_column(String(64))
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
