import uuid from datetime import datetime, timezone from typing import Optional, TYPE_CHECKING from sqlalchemy import String, Text, DateTime, ForeignKey, Boolean, Integer, UniqueConstraint from sqlalchemy.orm import Mapped, mapped_column, relationship from sqlalchemy.dialects.postgresql import UUID from app.core.database import Base if TYPE_CHECKING: from app.models.account import Account from app.models.tree import Tree from app.models.category import TreeCategory class Workspace(Base): """Workspaces are the top-level organizational context for trees/flows. They sit above the folder system — a workspace scopes which trees/flows are visible, while folders remain for personal organization within. """ __tablename__ = "workspaces" __table_args__ = ( UniqueConstraint('slug', 'account_id', name='uq_workspaces_slug_account'), ) id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 ) name: Mapped[str] = mapped_column(String(100), nullable=False) slug: Mapped[str] = mapped_column(String(100), nullable=False, index=True) description: Mapped[Optional[str]] = mapped_column(Text, nullable=True) icon: Mapped[Optional[str]] = mapped_column(String(10), nullable=True) accent_color: Mapped[Optional[str]] = mapped_column(String(7), nullable=True) account_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), nullable=False, index=True ) is_default: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False) sort_order: Mapped[int] = mapped_column(Integer, nullable=False, default=0) created_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=lambda: datetime.now(timezone.utc) ) updated_at: Mapped[datetime] = mapped_column( DateTime(timezone=True), default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc) ) # Relationships account: Mapped["Account"] = relationship("Account", back_populates="workspaces") trees: Mapped[list["Tree"]] = relationship("Tree", back_populates="workspace") categories: Mapped[list["TreeCategory"]] = relationship("TreeCategory", back_populates="workspace")