from logging import getLogger

from fastapi import status as st
from httpx import AsyncClient

from core.config import settings
from shared import exceptions as e

from . import schema as s


class VITACORE_API(AsyncClient):
    def __init__(self):
        self.logger = getLogger(__name__)
        super().__init__(base_url=settings.VITACORE_BASE_URL)

    async def findBySnils(self, snils: str):
        return

    async def getProfile(self, patId: str):
        req = await self.get('/getProfile', params={'patId': patId})

        match req.status_code:
            case st.HTTP_200_OK:
                return s.ProfileModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getDepartments(self):
        req = await self.get('/getDepartments')

        match req.status_code:
            case st.HTTP_200_OK:
                return [
                    s.DepartmentModel.model_validate(i) for i in req.json()
                ]
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getWorkers(self, departmentId: str):
        req = await self.get(
            '/getWorkers', params={'departmentId': departmentId}
        )

        match req.status_code:
            case st.HTTP_200_OK:
                return s.WorkersModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getSpecsV021(self):
        req = await self.get('/getSpecsV021')

        match req.status_code:
            case st.HTTP_200_OK:
                return s.SpecsV021Model.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getEntries(self, patId: str):
        req = await self.get('/getEntries', params={'patId': patId})

        match req.status_code:
            case st.HTTP_200_OK:
                return s.EntriesModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getVaccsReport(self, patId: str):
        req = await self.get('/getVaccsReport', params={'patId': patId})

        match req.status_code:
            case st.HTTP_200_OK:
                return s.VaccsReportModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getMedExamDict(self):
        req = await self.get('/getMedExamDict')

        match req.status_code:
            case st.HTTP_200_OK:
                return s.MedExamDictModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getRoutesList(self, patId: str):
        req = await self.get('/getRoutesList', params={'patId': patId})

        match req.status_code:
            case st.HTTP_200_OK:
                return s.RoutesListModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getHospExaminations(self, patId: str, examId: str):
        req = await self.get(
            '/getHospExaminations',
            params={'patId': patId, 'examId': examId},
        )

        match req.status_code:
            case st.HTTP_200_OK:
                return s.HospExaminationsModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getCurrHosp(self, patId: str):
        req = await self.get('/getCurrHosp', params={'patId': patId})

        match req.status_code:
            case st.HTTP_200_OK:
                return s.HospitalizationsModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getHosps(self, patId: str):
        req = await self.get('/getHosps', params={'patId': patId})

        match req.status_code:
            case st.HTTP_200_OK:
                return s.HospitalizationsModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException

    async def getHospRecommendations(self, patId: str):
        req = await self.get(
            '/getHospRecommendations', params={'patId': patId}
        )

        match req.status_code:
            case st.HTTP_200_OK:
                return s.HospRecommendationsModel.model_validate(req.json())
            case _:
                self.logger.error(req.json())
                raise e.UnknownException
