"""Doctor and Patient repositories."""
from __future__ import annotations

from uuid import UUID

from sqlalchemy import select

from app.models.profiles import Doctor, Patient
from app.repositories.base import TenantBaseRepository


class DoctorRepository(TenantBaseRepository[Doctor]):
    model = Doctor

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

    async def get_by_user_id(self, tenant_id: UUID, user_id: str) -> Doctor | None:
        stmt = select(self.model).where(
            self.model.tenant_id == tenant_id,
            self.model.user_id == user_id,
            self.model.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalar_one_or_none()


class PatientRepository(TenantBaseRepository[Patient]):
    model = Patient

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

    async def get_by_user_id(self, tenant_id: UUID, user_id: str) -> Patient | None:
        stmt = select(self.model).where(
            self.model.tenant_id == tenant_id,
            self.model.user_id == user_id,
            self.model.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalar_one_or_none()
