"""Arm care (N23-N25). No rest policy is configured, so the assessment is always "unknown".""" from datetime import UTC, date, datetime, timedelta from sqlalchemy.ext.asyncio import AsyncSession from app.common.academy_access import coaches_of_player from app.common.notify import notify from app.core.errors import ValidationFailed from app.integrations.push import PushClient from app.modules.accounts.models import Account from app.modules.arm_care.repository import FeelRepository, OutingRepository from app.modules.arm_care.schemas import ArmCareOut, FeelIn, FeelOut, OutingIn, OutingOut, OutingResult DEFAULT_DAYS = 30 MAX_DAYS = 366 def _outing(o) -> OutingOut: return OutingOut(id=o.id, date=o.created_at, local_date=o.local_date, label="Game, logged by you", pitches=o.pitches) class ArmCareService: def __init__(self, db: AsyncSession, push: PushClient | None = None): self.db = db self.push = push self.outings = OutingRepository(db) self.feels = FeelRepository(db) async def get(self, player: Account, start: date | None = None, end: date | None = None) -> ArmCareOut: today = datetime.now(UTC).date() end = end or today + timedelta(days=1) # a player ahead of UTC may already log tomorrow's date start = start or end - timedelta(days=DEFAULT_DAYS) if end < start: raise ValidationFailed(fields={"to": "Must be on or after from."}) if (end - start).days > MAX_DAYS: raise ValidationFailed(fields={"to": f"The range can be at most {MAX_DAYS} days."}) rows = await self.outings.in_range(player.id, start, end) latest = await self.feels.latest(player.id) return ArmCareOut(outings=[_outing(o) for o in rows], feel=latest.feel if latest else None, feel_recorded_at=latest.created_at if latest else None) async def add_outing(self, player: Account, body: OutingIn) -> OutingResult: today = datetime.now(UTC).date() local_date = body.local_date or today # Timezones are not modelled yet, so a player up to a day ahead of UTC may be on "tomorrow". if local_date > today + timedelta(days=1): raise ValidationFailed(fields={"local_date": "Must not be in the future."}) if (today - local_date).days > MAX_DAYS: raise ValidationFailed(fields={"local_date": f"Must be within the last {MAX_DAYS} days."}) outing = await self.outings.create(owner_id=player.id, local_date=local_date, pitches=body.pitches) name = player.name or "Your player" for coach_id in await coaches_of_player(self.db, player): await notify(self.db, self.push, coach_id, kind="armcare", title=f"{name} logged {body.pitches} pitches", target={}, event_key=f"outing:{outing.id}") end = max(local_date, today) start = max(min(local_date, today - timedelta(days=DEFAULT_DAYS)), end - timedelta(days=MAX_DAYS)) arm_care = await self.get(player, start, end) return OutingResult(outing=_outing(outing), arm_care=arm_care) async def set_feel(self, player: Account, body: FeelIn) -> FeelOut: await self.feels.lock_owner(player.id) latest = await self.feels.latest(player.id) if latest is not None and latest.feel == body.feel and latest.created_at.date() == datetime.now(UTC).date(): return FeelOut(feel=latest.feel, recorded_at=latest.created_at) event = await self.feels.create(owner_id=player.id, feel=body.feel) return FeelOut(feel=event.feel, recorded_at=event.created_at)