import uuid from datetime import date from sqlalchemy import CheckConstraint, Date, ForeignKey, Index, SmallInteger, String, UniqueConstraint, text from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Mapped, mapped_column from app.common.base_model import Base, CreatedAtMixin, IdMixin class DrillAssignment(IdMixin, CreatedAtMixin, Base): """A drill a coach prescribed to one player. name/fixes are snapshots; progress is the count of its sets.""" __tablename__ = "drill_assignments" __table_args__ = ( CheckConstraint("mode IN ('h', 'p')", name="mode"), CheckConstraint("drill_source IN ('catalog', 'library')", name="drill_source"), CheckConstraint("sets BETWEEN 1 AND 10", name="sets"), CheckConstraint("reps BETWEEN 1 AND 8", name="reps"), Index("ix_drill_assignments_player_id_mode_created_at", "player_id", "mode", text("created_at DESC")), ) team_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("teams.id", ondelete="SET NULL")) player_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE")) assigned_by_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="SET NULL") ) mode: Mapped[str] = mapped_column(String(1)) drill_source: Mapped[str] = mapped_column(String(8)) drill_id: Mapped[str] = mapped_column(String(40)) name: Mapped[str] = mapped_column(String(120)) fixes: Mapped[str] = mapped_column(String(500), server_default="") sets: Mapped[int] = mapped_column(SmallInteger, server_default="3") reps: Mapped[int] = mapped_column(SmallInteger, server_default="8") due_date: Mapped[date] = mapped_column(Date) session_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("sessions.id", ondelete="SET NULL") ) class AssignmentSet(IdMixin, CreatedAtMixin, Base): """One completed set of an assignment; drill_log_id is the practice log written in the same transaction.""" __tablename__ = "assignment_sets" __table_args__ = (UniqueConstraint("assignment_id", "set_number"),) assignment_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("drill_assignments.id", ondelete="CASCADE") ) set_number: Mapped[int] = mapped_column(SmallInteger) reps_done: Mapped[int] = mapped_column(SmallInteger) day: Mapped[date] = mapped_column(Date) drill_log_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("drill_logs.id", ondelete="SET NULL") )