"""Clinic management repositories — Phase 13.3."""
from __future__ import annotations

from uuid import UUID

from sqlalchemy import select

from app.models.clinic_management import (
    ClinicPolicy,
    ClinicService,
    OperatingHoursPolicy,
    StaffAssignment,
)
from app.repositories.base import TenantBaseRepository


class ClinicServiceRepository(TenantBaseRepository[ClinicService]):
    model = ClinicService

    async def get_by_code(self, tenant_id: UUID, code: str) -> ClinicService | None:
        stmt = select(ClinicService).where(
            ClinicService.tenant_id == tenant_id,
            ClinicService.code == code,
            ClinicService.is_deleted.is_(False),
        )
        return (await self.session.execute(stmt)).scalar_one_or_none()


class StaffAssignmentRepository(TenantBaseRepository[StaffAssignment]):
    model = StaffAssignment

    async def get_by_code(self, tenant_id: UUID, code: str) -> StaffAssignment | None:
        stmt = select(StaffAssignment).where(
            StaffAssignment.tenant_id == tenant_id,
            StaffAssignment.code == code,
            StaffAssignment.is_deleted.is_(False),
        )
        return (await self.session.execute(stmt)).scalar_one_or_none()


class ClinicPolicyRepository(TenantBaseRepository[ClinicPolicy]):
    model = ClinicPolicy

    async def get_by_code(self, tenant_id: UUID, code: str) -> ClinicPolicy | None:
        stmt = select(ClinicPolicy).where(
            ClinicPolicy.tenant_id == tenant_id,
            ClinicPolicy.code == code,
            ClinicPolicy.is_deleted.is_(False),
        )
        return (await self.session.execute(stmt)).scalar_one_or_none()


class OperatingHoursPolicyRepository(TenantBaseRepository[OperatingHoursPolicy]):
    model = OperatingHoursPolicy

    async def get_by_code(self, tenant_id: UUID, code: str) -> OperatingHoursPolicy | None:
        stmt = select(OperatingHoursPolicy).where(
            OperatingHoursPolicy.tenant_id == tenant_id,
            OperatingHoursPolicy.code == code,
            OperatingHoursPolicy.is_deleted.is_(False),
        )
        return (await self.session.execute(stmt)).scalar_one_or_none()
