"""Membership Catalog repositories — Phase 9.1."""
from __future__ import annotations

from uuid import UUID

from sqlalchemy import select

from app.models.catalog import (
    AgeGroup,
    ExpirationPolicy,
    FreezingRule,
    MembershipPackage,
    MembershipPlan,
    MembershipRule,
    PricingModel,
    RenewalPolicy,
    SportCategory,
)
from app.repositories.base import TenantBaseRepository


class _CenterCodedRepository(TenantBaseRepository):
    async def get_by_code(
        self, tenant_id: UUID, sports_center_id: UUID, code: str
    ):
        model = self.model
        stmt = select(model).where(
            model.tenant_id == tenant_id,
            model.sports_center_id == sports_center_id,
            model.code == code,
            model.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalar_one_or_none()

    async def list_by_center(
        self, tenant_id: UUID, sports_center_id: UUID, *, offset: int = 0, limit: int = 50
    ):
        model = self.model
        stmt = (
            select(model)
            .where(
                model.tenant_id == tenant_id,
                model.sports_center_id == sports_center_id,
                model.is_deleted.is_(False),
            )
            .offset(offset)
            .limit(limit)
        )
        result = await self.session.execute(stmt)
        return result.scalars().all()


class SportCategoryRepository(_CenterCodedRepository):
    model = SportCategory


class AgeGroupRepository(_CenterCodedRepository):
    model = AgeGroup


class PricingModelRepository(_CenterCodedRepository):
    model = PricingModel


class MembershipPackageRepository(_CenterCodedRepository):
    model = MembershipPackage


class MembershipPlanRepository(_CenterCodedRepository):
    model = MembershipPlan


class MembershipRuleRepository(_CenterCodedRepository):
    model = MembershipRule


class RenewalPolicyRepository(_CenterCodedRepository):
    model = RenewalPolicy


class FreezingRuleRepository(_CenterCodedRepository):
    model = FreezingRule


class ExpirationPolicyRepository(_CenterCodedRepository):
    model = ExpirationPolicy
