from __future__ import annotations from datetime import datetime from typing import Any from sqlalchemy import DateTime, ForeignKey, Integer, JSON, String, func from sqlalchemy.orm import Mapped, mapped_column from app.core.database import Base from app.utils.ids import prefixed_id class StudentWorkspace(Base): """Versioned cross-surface state owned by one authenticated student. Dedicated domain tables remain authoritative for documents, chat, tuition, roadmaps, jobs, and billing. This aggregate only fills the small state gaps shared by student-facing pages such as tasks, notes, bookmarks, and alerts. """ __tablename__ = "student_workspaces" id: Mapped[str] = mapped_column( String(40), primary_key=True, default=lambda: prefixed_id("sws"), ) user_id: Mapped[str] = mapped_column( String(40), ForeignKey("users.id", ondelete="CASCADE"), index=True, unique=True, nullable=False, ) revision: Mapped[int] = mapped_column(Integer, default=0, nullable=False) data: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict, nullable=False) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), nullable=False, ) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False, )