"""document_notes 테이블 ORM — 자료별 손글씨 노트 (자료 1:1).

설계:
  - user×document UNIQUE — 자료당 사용자별 한 캔버스.
  - upsert 방식. PUT /api/documents/{id}/note 로 strokes_json 전체 갱신.
  - 회독 (document_reads, append-only log) 와 별개.

NOTE: documents 에 user_id 부재 (single-user). document_notes.user_id 로
ownership. multi-user 전환 시 documents.user_id 추가 후 별도 check 필요.
"""

from datetime import datetime
from typing import Any

from sqlalchemy import BigInteger, DateTime, ForeignKey, Integer, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped, mapped_column

from core.database import Base


class DocumentNote(Base):
    __tablename__ = "document_notes"
    __table_args__ = (
        UniqueConstraint("user_id", "document_id", name="document_notes_user_id_document_id_key"),
    )

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    user_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
    )
    document_id: Mapped[int] = mapped_column(
        BigInteger, ForeignKey("documents.id", ondelete="CASCADE"), nullable=False
    )
    strokes_json: Mapped[dict[str, Any] | None] = mapped_column(JSONB)
    canvas_width: Mapped[int | None] = mapped_column(Integer)
    canvas_height: Mapped[int | None] = mapped_column(Integer)
    schema_version: Mapped[int] = mapped_column(Integer, default=1, nullable=False)
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now, nullable=False
    )
    updated_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), default=datetime.now, onupdate=datetime.now, nullable=False
    )
