"""Academy (= an existing team) staff and organisation. The team's owner (teams.coach_id) is the implicit admin; other staff have a membership row. Players belong through accounts.team_id as before. """ import uuid from datetime import datetime from sqlalchemy import ( Boolean, CheckConstraint, DateTime, ForeignKey, Index, Integer, SmallInteger, String, Text, UniqueConstraint, text, ) from sqlalchemy.dialects.postgresql import ARRAY, JSONB, UUID from sqlalchemy.orm import Mapped, mapped_column from app.common.base_model import Base, CreatedAtMixin, IdMixin, TimestampMixin def _team() -> Mapped[uuid.UUID]: return mapped_column(UUID(as_uuid=True), ForeignKey("teams.id", ondelete="CASCADE"), index=True) def _account(nullable: bool = False) -> Mapped: return mapped_column(UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="SET NULL" if nullable else "CASCADE")) class AcademyMembership(IdMixin, CreatedAtMixin, Base): """A staff member of a team. permissions: {review, drills, goals, library, earnings} effective booleans.""" __tablename__ = "academy_memberships" __table_args__ = ( UniqueConstraint("team_id", "account_id"), CheckConstraint("role IN ('admin', 'coach', 'assistant')", name="role"), CheckConstraint("status IN ('active', 'paused', 'removed')", name="status"), Index("ix_academy_memberships_account_id", "account_id"), ) team_id: Mapped[uuid.UUID] = _team() account_id: Mapped[uuid.UUID] = _account() role: Mapped[str] = mapped_column(String(10)) status: Mapped[str] = mapped_column(String(8), server_default="active") sees_all: Mapped[bool] = mapped_column(Boolean, server_default="false") permissions: Mapped[dict] = mapped_column(JSONB) joined_with: Mapped[str | None] = mapped_column(String(16)) version: Mapped[int] = mapped_column(Integer, server_default="1") class StaffCode(IdMixin, CreatedAtMixin, Base): """A code staff use to apply. uses is a counter locked with the row so the last use cannot be taken twice.""" __tablename__ = "staff_codes" __table_args__ = ( CheckConstraint("role IN ('coach', 'assistant')", name="role"), CheckConstraint("approval IN ('admin', 'auto')", name="approval"), CheckConstraint("uses >= 0 AND uses <= max_uses", name="uses"), ) team_id: Mapped[uuid.UUID] = _team() code: Mapped[str] = mapped_column(String(16), unique=True) role: Mapped[str] = mapped_column(String(10)) expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True)) max_uses: Mapped[int] = mapped_column(SmallInteger) uses: Mapped[int] = mapped_column(SmallInteger, server_default="0") approval: Mapped[str] = mapped_column(String(6)) revoked_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) created_by_id: Mapped[uuid.UUID | None] = _account(nullable=True) class CoachAccessRequest(IdMixin, CreatedAtMixin, Base): """An application to coach at a team. created_at is sent_at. At most one pending request per applicant.""" __tablename__ = "coach_access_requests" __table_args__ = ( CheckConstraint("status IN ('pending', 'approved', 'declined')", name="status"), Index("uq_coach_access_requests_pending", "applicant_id", unique=True, postgresql_where=text("status = 'pending'")), Index("ix_coach_access_requests_team_id_created_at", "team_id", text("created_at DESC")), ) applicant_id: Mapped[uuid.UUID] = _account() team_id: Mapped[uuid.UUID] = _team() staff_code_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("staff_codes.id", ondelete="SET NULL") ) code: Mapped[str] = mapped_column(String(16)) status: Mapped[str] = mapped_column(String(8), server_default="pending") decided_by_id: Mapped[uuid.UUID | None] = _account(nullable=True) decided_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True)) class PlayerGroup(IdMixin, CreatedAtMixin, Base): __tablename__ = "player_groups" __table_args__ = ( CheckConstraint("focus IN ('Hitting', 'Pitching', 'Both')", name="focus"), Index("uq_player_groups_team_id_name", "team_id", text("lower(name)"), unique=True), ) team_id: Mapped[uuid.UUID] = _team() name: Mapped[str] = mapped_column(String(80)) focus: Mapped[str] = mapped_column(String(8)) class GroupPlayer(Base): __tablename__ = "group_players" group_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("player_groups.id", ondelete="CASCADE"), primary_key=True ) player_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), primary_key=True, index=True ) class GroupCoach(Base): __tablename__ = "group_coaches" group_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("player_groups.id", ondelete="CASCADE"), primary_key=True ) coach_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), primary_key=True, index=True ) class CoachPlayer(CreatedAtMixin, Base): """A player on one staff coach's own roster (in addition to group and sees_all visibility).""" __tablename__ = "coach_players" team_id: Mapped[uuid.UUID] = _team() coach_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), primary_key=True ) player_id: Mapped[uuid.UUID] = mapped_column( UUID(as_uuid=True), ForeignKey("accounts.id", ondelete="CASCADE"), primary_key=True, index=True ) class CoachNote(IdMixin, TimestampMixin, Base): """A coach's private note on a player; only that coach can read it.""" __tablename__ = "coach_notes" __table_args__ = (UniqueConstraint("team_id", "coach_id", "player_id"),) team_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("teams.id", ondelete="CASCADE")) coach_id: Mapped[uuid.UUID] = _account() player_id: Mapped[uuid.UUID] = _account() text: Mapped[str] = mapped_column(Text, server_default="") version: Mapped[int] = mapped_column(Integer, server_default="0") class LibraryDrill(IdMixin, CreatedAtMixin, Base): """A team's own drill video. Assignments snapshot name/fixes, so deleting one never rewrites history.""" __tablename__ = "library_drills" __table_args__ = (CheckConstraint("mode IN ('h', 'p')", name="mode"),) team_id: Mapped[uuid.UUID] = _team() author_id: Mapped[uuid.UUID | None] = _account(nullable=True) name: Mapped[str] = mapped_column(String(120)) fixes: Mapped[str] = mapped_column(String(500), server_default="") mode: Mapped[str] = mapped_column(String(1)) fault_tags: Mapped[list[str]] = mapped_column(ARRAY(String(64)), server_default=text("'{}'")) upload_id: Mapped[uuid.UUID | None] = mapped_column( UUID(as_uuid=True), ForeignKey("uploads.id", ondelete="SET NULL") )