"""Medical record repositories — Phase 13.5."""
from __future__ import annotations

from uuid import UUID

from sqlalchemy import select

from app.models.medical_records import (
    Allergy,
    Condition,
    Encounter,
    ImmunizationRecord,
    MedicalRecord,
    MedicalRecordAccessLog,
    MedicationStatement,
)
from app.repositories.base import TenantBaseRepository


class MedicalRecordRepository(TenantBaseRepository[MedicalRecord]):
    model = MedicalRecord

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


class EncounterRepository(TenantBaseRepository[Encounter]):
    model = Encounter

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

    async def list_by_record(self, tenant_id: UUID, medical_record_id: UUID):
        stmt = select(Encounter).where(
            Encounter.tenant_id == tenant_id,
            Encounter.medical_record_id == medical_record_id,
            Encounter.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalars().all()


class ConditionRepository(TenantBaseRepository[Condition]):
    model = Condition

    async def list_by_record(self, tenant_id: UUID, medical_record_id: UUID):
        stmt = select(Condition).where(
            Condition.tenant_id == tenant_id,
            Condition.medical_record_id == medical_record_id,
            Condition.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalars().all()


class AllergyRepository(TenantBaseRepository[Allergy]):
    model = Allergy

    async def list_by_record(self, tenant_id: UUID, medical_record_id: UUID):
        stmt = select(Allergy).where(
            Allergy.tenant_id == tenant_id,
            Allergy.medical_record_id == medical_record_id,
            Allergy.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalars().all()


class MedicationStatementRepository(TenantBaseRepository[MedicationStatement]):
    model = MedicationStatement

    async def list_by_record(self, tenant_id: UUID, medical_record_id: UUID):
        stmt = select(MedicationStatement).where(
            MedicationStatement.tenant_id == tenant_id,
            MedicationStatement.medical_record_id == medical_record_id,
            MedicationStatement.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalars().all()


class ImmunizationRecordRepository(TenantBaseRepository[ImmunizationRecord]):
    model = ImmunizationRecord

    async def list_by_record(self, tenant_id: UUID, medical_record_id: UUID):
        stmt = select(ImmunizationRecord).where(
            ImmunizationRecord.tenant_id == tenant_id,
            ImmunizationRecord.medical_record_id == medical_record_id,
            ImmunizationRecord.is_deleted.is_(False),
        )
        result = await self.session.execute(stmt)
        return result.scalars().all()


class MedicalRecordAccessLogRepository(TenantBaseRepository[MedicalRecordAccessLog]):
    model = MedicalRecordAccessLog

    async def list_by_record(self, tenant_id: UUID, medical_record_id: UUID):
        stmt = select(MedicalRecordAccessLog).where(
            MedicalRecordAccessLog.tenant_id == tenant_id,
            MedicalRecordAccessLog.medical_record_id == medical_record_id,
        )
        result = await self.session.execute(stmt)
        return result.scalars().all()
