"""Competition repositories — Phase 9.7."""
from __future__ import annotations

from uuid import UUID

from sqlalchemy import select

from app.models.competitions import (
    Competition,
    CompetitionCertificate,
    CompetitionGroup,
    CompetitionJudge,
    CompetitionMatch,
    CompetitionMedal,
    CompetitionRanking,
    CompetitionRegistration,
    CompetitionResult,
    CompetitionTeam,
)
from app.repositories.base import TenantBaseRepository


class CompetitionRepository(TenantBaseRepository[Competition]):
    model = Competition


class CompetitionTeamRepository(TenantBaseRepository[CompetitionTeam]):
    model = CompetitionTeam

    async def list_by_competition(self, tenant_id: UUID, competition_id: UUID, *, offset=0, limit=20):
        stmt = (
            select(self.model)
            .where(
                self.model.tenant_id == tenant_id,
                self.model.competition_id == competition_id,
                self.model.is_deleted.is_(False),
            )
            .offset(offset)
            .limit(limit)
        )
        return (await self.session.execute(stmt)).scalars().all()

    async def count_by_competition(self, tenant_id: UUID, competition_id: UUID) -> int:
        from sqlalchemy import func

        stmt = select(func.count()).select_from(self.model).where(
            self.model.tenant_id == tenant_id,
            self.model.competition_id == competition_id,
            self.model.is_deleted.is_(False),
        )
        return (await self.session.execute(stmt)).scalar_one()


class CompetitionGroupRepository(TenantBaseRepository[CompetitionGroup]):
    model = CompetitionGroup


class CompetitionRegistrationRepository(TenantBaseRepository[CompetitionRegistration]):
    model = CompetitionRegistration


class CompetitionMatchRepository(TenantBaseRepository[CompetitionMatch]):
    model = CompetitionMatch


class CompetitionResultRepository(TenantBaseRepository[CompetitionResult]):
    model = CompetitionResult


class CompetitionRankingRepository(TenantBaseRepository[CompetitionRanking]):
    model = CompetitionRanking

    async def list_by_competition(self, tenant_id: UUID, competition_id: UUID, *, offset=0, limit=20):
        stmt = (
            select(self.model)
            .where(
                self.model.tenant_id == tenant_id,
                self.model.competition_id == competition_id,
                self.model.is_deleted.is_(False),
            )
            .order_by(self.model.rank)
            .offset(offset)
            .limit(limit)
        )
        return (await self.session.execute(stmt)).scalars().all()

    async def get_by_team(self, tenant_id: UUID, competition_id: UUID, team_id: UUID):
        stmt = select(self.model).where(
            self.model.tenant_id == tenant_id,
            self.model.competition_id == competition_id,
            self.model.team_id == team_id,
            self.model.is_deleted.is_(False),
        )
        return (await self.session.execute(stmt)).scalar_one_or_none()


class CompetitionMedalRepository(TenantBaseRepository[CompetitionMedal]):
    model = CompetitionMedal


class CompetitionJudgeRepository(TenantBaseRepository[CompetitionJudge]):
    model = CompetitionJudge


class CompetitionCertificateRepository(TenantBaseRepository[CompetitionCertificate]):
    model = CompetitionCertificate
