from __future__ import annotations

from ..exceptions import BrightreeException
from .base import BaseService


class Patient(BaseService):
    methods = {
        "FacilityMasterInfoFetchAll": False,
        "FacilityResidentCreate": True,
        "PatientCreate": True,
        "PatientSearch": True,
        "PatientUpdate": True,
        "PatientNoteCreate": True,
        "PatientNoteSearch": True,
        "PatientNoteUpdate": True,
        "PatientPayorAdd": True,
        "PatientPayorFetch": True,
        "PatientPayorUpdate": True,
        "PatientPhoneNumberSearch": True,
        "PatientUpdateSleepTherapyPatientID": True,
        "PharmacyPatientClinicalInfoFetchByBrightreeID": True,
        "FetchPatientOptInStatus": True,
        "UpdatePatientOptInStatus": True,
    }
    special_methods = {
        "PatientAddMarketingReferral": ("BrightreeID", "BrightreeReferralID"),
        "PatientFetchByExternalID": ("ExternalID",),
        "PatientFetchByPatientID": ("PatientID",),
        "PatientFetchByBrightreeID": ("BrightreeID",),
        "PatientNoteFetchByKey": ("brightreeID",),
        "PatientNoteFetchByPatient": ("brightreeID",),
        "PatientPayorFetchAll": ("PatientKey",),
        "PatientPayorRemove": ("brightreeID",),
        "PatientRemoveMarketingReferral": ("brightreeID",),
        "PharmacyPatientLabResultsFetchByBrightreeIDAndPatientBrightreeID": (
            "BrightreeID",
            "PatientBrightreeID",
        ),
        "PharmacyPatientMedicationHistoryFetchByBrightreeIDAndPatientBrightreeID": (
            "BrightreeID",
            "PatientBrightreeID",
        ),
        "PharmacyPatientMostRecentLabResultsFetchByPatientBrightreeID": ("brightreeID",),
    }

    def PatientFetchByBrightreeID(self, brightree_id: int):
        if brightree_id <= 0:
            raise BrightreeException(f"Invalid Brightree ID: {brightree_id}", 1003)
        return self._api_call("PatientFetchByBrightreeID", {"BrightreeID": brightree_id})

    def PatientAddMarketingReferral(self, brightree_id: int | None = None, referral_id: int | None = None):
        if brightree_id is None:
            raise BrightreeException("BrightreeID is required for PatientAddMarketingReferral", 1003)
        if referral_id is None:
            raise BrightreeException("BrightreeReferralID is required for PatientAddMarketingReferral", 1003)
        return self._api_call(
            "PatientAddMarketingReferral",
            {"BrightreeID": brightree_id, "BrightreeReferralID": referral_id},
        )

    def PatientPayorFetchAll(self, patient_key: int):
        if patient_key <= 0:
            raise BrightreeException(f"Invalid PatientKey: {patient_key}", 1003)
        return self._api_call("PatientPayorFetchAll", {"PatientKey": patient_key})

    def PharmacyPatientLabResultsFetchByBrightreeIDAndPatientBrightreeID(
        self,
        patient_brightree_id: int | None = None,
        brightree_id: int | None = None,
    ):
        if patient_brightree_id is None:
            raise BrightreeException("PatientBrightreeID is required", 1003)
        if brightree_id is None:
            raise BrightreeException("BrightreeID is required", 1003)
        return self._api_call(
            "PharmacyPatientLabResultsFetchByBrightreeIDAndPatientBrightreeID",
            {"BrightreeID": brightree_id, "PatientBrightreeID": patient_brightree_id},
        )
