import uuid from datetime import datetime from sqlalchemy import Boolean, CheckConstraint, DateTime, ForeignKey, Index, Integer, String, Text from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Mapped, mapped_column from app.common.base_model import Base, CreatedAtMixin, IdMixin, TimestampMixin def _flag(default: bool) -> Mapped[bool]: return mapped_column(Boolean, server_default="true" if default else "false") class UserSettings(TimestampMixin, Base): """Sharing and notification preferences. A missing row means the defaults; effective consent lives in events.""" __tablename__ = "user_settings" account_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), primary_key=True ) coach_can_see_sessions: Mapped[bool] = _flag(True) share_with_parent: Mapped[bool] = _flag(False) parent_email: Mapped[str | None] = mapped_column(String(254)) auto_monthly_report: Mapped[bool] = _flag(False) private_links: Mapped[bool] = _flag(False) clips_as_examples: Mapped[bool] = _flag(False) help_improve_ai: Mapped[bool] = _flag(False) notify_results: Mapped[bool] = _flag(True) notify_coach: Mapped[bool] = _flag(True) notify_drills: Mapped[bool] = _flag(True) notify_weekly: Mapped[bool] = _flag(True) skip_filming_guide: Mapped[bool] = _flag(False) timezone: Mapped[str | None] = mapped_column(String(64)) version: Mapped[int] = mapped_column(Integer, server_default="1") class ConsentEvent(IdMixin, CreatedAtMixin, Base): """Append-only consent history. The latest 'analysis' event decides whether new analysis is allowed.""" __tablename__ = "consent_events" __table_args__ = ( CheckConstraint("category IN ('analysis', 'help_improve_ai', 'clips_as_examples')", name="category"), CheckConstraint("action IN ('accepted', 'withdrawn')", name="action"), Index("ix_consent_events_account_id_category_created_at", "account_id", "category", "created_at"), ) account_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE")) category: Mapped[str] = mapped_column(String(20)) action: Mapped[str] = mapped_column(String(10)) policy_version: Mapped[str | None] = mapped_column(String(16)) class ExportJob(IdMixin, CreatedAtMixin, Base): """A requested export (all videos for a player, consent CSV for an academy).""" __tablename__ = "export_jobs" __table_args__ = ( CheckConstraint("scope IN ('videos', 'consent')", name="scope"), CheckConstraint("status IN ('queued', 'ready', 'failed', 'cancelled')", name="status"), Index("ix_export_jobs_requester_id_status", "requester_id", "status"), ) requester_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE") ) team_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("teams.id", ondelete="CASCADE")) scope: Mapped[str] = mapped_column(String(10)) status: Mapped[str] = mapped_column(String(10), server_default="queued") object_key: Mapped[str | None] = mapped_column(Text) filename: Mapped[str | None] = mapped_column(String(120)) error_code: Mapped[str | None] = mapped_column(String(40)) expires_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))