import uuid from datetime import datetime from decimal import Decimal from sqlalchemy import String, Text, TIMESTAMP, Numeric, ForeignKey, text from sqlalchemy.dialects.postgresql import UUID, ARRAY, JSONB from sqlalchemy.orm import Mapped, mapped_column, relationship from app.core.database import Base class NoteItem(Base): __tablename__ = "items" __table_args__ = {"schema": "notes"} id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) title: Mapped[str | None] = mapped_column(String(255)) content: Mapped[str] = mapped_column(Text, nullable=False) category: Mapped[str | None] = mapped_column(String(50)) tags: Mapped[list[str]] = mapped_column(ARRAY(String(50)), server_default=text("'{}'::varchar[]")) gps_lat: Mapped[Decimal | None] = mapped_column(Numeric(10, 7)) gps_lon: Mapped[Decimal | None] = mapped_column(Numeric(10, 7)) metadata_: Mapped[dict | None] = mapped_column("metadata", JSONB) created_at: Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True), server_default=text("now()")) owner_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True)) attachments: Mapped[list["NoteAttachment"]] = relationship("NoteAttachment", back_populates="note", cascade="all, delete-orphan") class NoteAttachment(Base): __tablename__ = "attachments" __table_args__ = {"schema": "notes"} id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) note_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("notes.items.id", ondelete="CASCADE"), nullable=False) file_path: Mapped[str | None] = mapped_column(String(255)) thumbnail_path: Mapped[str | None] = mapped_column(String(255)) file_type: Mapped[str | None] = mapped_column(String(20)) original_name: Mapped[str | None] = mapped_column(String(255)) created_at: Mapped[datetime] = mapped_column(TIMESTAMP(timezone=True), server_default=text("now()")) note: Mapped["NoteItem"] = relationship("NoteItem", back_populates="attachments")