from __future__ import annotations import uuid from sqlalchemy import select from sqlalchemy.orm import selectinload from rehearsalhub.db.models import AudioVersion, Song from rehearsalhub.repositories.base import BaseRepository class SongRepository(BaseRepository[Song]): model = Song async def list_for_band(self, band_id: uuid.UUID) -> list[Song]: stmt = ( select(Song) .where(Song.band_id == band_id) .options(selectinload(Song.versions)) .order_by(Song.updated_at.desc()) ) result = await self.session.execute(stmt) return list(result.scalars().all()) async def get_with_versions(self, song_id: uuid.UUID) -> Song | None: stmt = ( select(Song) .options(selectinload(Song.versions)) .where(Song.id == song_id) ) result = await self.session.execute(stmt) return result.scalar_one_or_none() async def get_by_nc_folder_path(self, nc_folder_path: str) -> "Song | None": stmt = select(Song).where(Song.nc_folder_path == nc_folder_path) result = await self.session.execute(stmt) return result.scalar_one_or_none() async def get_by_title_and_band(self, band_id: uuid.UUID, title: str) -> "Song | None": stmt = select(Song).where(Song.band_id == band_id, Song.title == title) result = await self.session.execute(stmt) return result.scalar_one_or_none() async def next_version_number(self, song_id: uuid.UUID) -> int: from sqlalchemy import func stmt = select(func.coalesce(func.max(AudioVersion.version_number), 0) + 1).where( AudioVersion.song_id == song_id ) result = await self.session.execute(stmt) return result.scalar_one()