import uuid from datetime import datetime from decimal import Decimal from sqlalchemy import CheckConstraint, DateTime, ForeignKey, Index, Integer, Numeric, SmallInteger, String, text from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import Mapped, mapped_column from app.common.base_model import Base, CreatedAtMixin, IdMixin class Goal(IdMixin, CreatedAtMixin, Base): """A coach-set goal. The active goal per (player, mode) has ended_at NULL; replaced goals are the history. created_at is set_at. outcome: met | not_met | superseded | unknown (NULL while active). """ __tablename__ = "goals" __table_args__ = ( CheckConstraint("mode IN ('h', 'p')", name="mode"), CheckConstraint("type IN ('groups', 'param', 'score')", name="type"), CheckConstraint("outcome IS NULL OR outcome IN ('met', 'not_met', 'superseded', 'unknown')", name="outcome"), Index("uq_goals_active", "player_id", "mode", unique=True, postgresql_where=text("ended_at IS NULL")), Index("ix_goals_player_id_mode_created_at", "player_id", "mode", text("created_at DESC")), ) player_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="SET NULL")) mode: Mapped[str] = mapped_column(String(1)) type: Mapped[str] = mapped_column(String(8)) target: Mapped[Decimal] = mapped_column(Numeric(8, 2)) metric_id: Mapped[str | None] = mapped_column(String(64)) due: Mapped[str] = mapped_column(String(16)) due_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) weekly_practice_days: Mapped[int] = mapped_column(SmallInteger) weekly_drill_sets: Mapped[int] = mapped_column(SmallInteger) note: Mapped[str] = mapped_column(String(160), server_default="") set_by_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="SET NULL") ) ended_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) outcome: Mapped[str | None] = mapped_column(String(10)) evaluated_session_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("sessions.id", ondelete="SET NULL") ) version: Mapped[int] = mapped_column(Integer, server_default="1")