import uuid from decimal import Decimal from sqlalchemy import Boolean, CheckConstraint, ForeignKey, Numeric, SmallInteger, String, Text from sqlalchemy.dialects.postgresql import CITEXT, UUID from sqlalchemy.orm import Mapped, mapped_column from app.common.base_model import Base, IdMixin, TimestampMixin ROLES = ("player", "coach") FOCUSES = ("h", "p", "both") LEVELS = ("youth", "high_school", "college", "adult") UNITS = ("imperial", "metric") def _in(column: str, values: tuple[str, ...]) -> str: return f"{column} IN ({', '.join(repr(v) for v in values)})" class Account(IdMixin, TimestampMixin, Base): """A player or coach. The athlete profile is embedded; a player's team is team_id (a coach's is teams.coach_id).""" __tablename__ = "accounts" __table_args__ = ( CheckConstraint(_in("role", ROLES), name="role"), CheckConstraint(_in("focus", FOCUSES), name="focus"), CheckConstraint(_in("unit", UNITS), name="unit"), CheckConstraint(f"level IS NULL OR {_in('level', LEVELS)}", name="level"), CheckConstraint("bats IS NULL OR bats IN ('L', 'R', 'S')", name="bats"), CheckConstraint("throws IS NULL OR throws IN ('L', 'R')", name="throws"), ) email: Mapped[str] = mapped_column(CITEXT, unique=True) password_hash: Mapped[str | None] = mapped_column(Text) role: Mapped[str] = mapped_column(String(8)) focus: Mapped[str] = mapped_column(String(4), server_default="both") name: Mapped[str] = mapped_column(String(80), server_default="") age: Mapped[int | None] = mapped_column(SmallInteger) height_cm: Mapped[Decimal | None] = mapped_column(Numeric(5, 1)) weight_kg: Mapped[Decimal | None] = mapped_column(Numeric(5, 1)) bats: Mapped[str | None] = mapped_column(String(1)) throws: Mapped[str | None] = mapped_column(String(1)) level: Mapped[str | None] = mapped_column(String(12)) unit: Mapped[str] = mapped_column(String(8), server_default="imperial") private_videos: Mapped[bool] = mapped_column(Boolean, server_default="true") team_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("teams.id", ondelete="SET NULL", use_alter=True), index=True )