from datetime import datetime
from json import dumps
from logging import getLogger
from typing import Annotated

from fastapi import APIRouter, Body, Depends, status

from apps.users.auth import login
from clients import clients as c
from clients.vitacore import schema as s
from shared.redis import client as cache

from . import mock

logger = getLogger(__name__)
router = APIRouter(
    prefix='/user',
    tags=[
        'User',
    ],
)


@router.get('/getProfile', response_model=s.ProfileModel)
async def get_profile():
    """
    Get profile of user by id.
    """
    return await c.vitacore_api.getProfile(
        'b62e9f22-a871-4c52-96d6-559c707a716d'
    )


@router.get('/getDepartments', response_model=list[s.DepartmentModel])
async def get_departments():
    """
    Get list of departments.
    """
    return await c.vitacore_api.getDepartments()


@router.get('/getWorkers', response_model=s.WorkersModel)
async def get_workers(departmentId: str):
    """
    Get list of workers by department id.
    """
    return await c.vitacore_api.getWorkers(departmentId)


@router.get('/getSpecs', response_model=s.SpecsV021Model)
async def get_specs():
    """
    Get list of specialties.
    """
    return await c.vitacore_api.getSpecsV021()


@router.get('/getEntries', response_model=s.EntriesModel)
async def get_entries():
    """
    Get list of entries for user by id.
    """
    return await c.vitacore_api.getEntries(
        'b172ddc1-bd94-407f-885f-725193dcc502'
    )


@router.get('/getVaccsReport')
async def get_vaccs_report():
    """
    Get report of vaccinations for user by id.
    """
    return await c.vitacore_api.getVaccsReport(
        '6fe66cae-409a-4f56-8ae9-d55d3c38569b'
    )


@router.get('/getMedExamDict')
async def get_med_exam_dict():
    """
    Get medical examination dictionary.
    """
    return await c.vitacore_api.getMedExamDict()


@router.get('/getRoutesList')
async def get_routes_list():
    """
    Get list of routes.
    """
    return await c.vitacore_api.getRoutesList(
        '4e6de5f7-4dc9-451b-bf0d-7a64a9b8c279'
    )


@router.get('/getHospExaminations')
async def get_hosp_examinations():
    """
    Get list of hospital examinations.
    """
    return await c.vitacore_api.getHospExaminations(
        '7bbdac30-9a33-4f13-9458-2c229c0c20f5',
        'f22be2c9-8e68-42d6-851e-fbf4a5e8f657',
    )


@router.get('/getCurrHosp')
async def get_curr_hosp():
    """
    Get current hospitalization.
    """
    return await c.vitacore_api.getCurrHosp(
        'b708e782-4f83-4f3b-8639-512c0c9637bf'
    )


@router.get('/getHosps')
async def get_hosps():
    """
    Get list of hospitals.
    """
    return await c.vitacore_api.getHosps(
        'b708e782-4f83-4f3b-8639-512c0c9637bf'
    )


@router.get('/getHospRecommendations')
async def get_hosp_recommendations():
    """
    Get list of recommended hospitals.
    """
    return await c.vitacore_api.getHospRecommendations(
        'b708e782-4f83-4f3b-8639-512c0c9637bf'
    )


@router.post('/measurement', status_code=status.HTTP_202_ACCEPTED)
async def measurement(
    user: Annotated[str, Depends(login)],
    ad: Annotated[int, Body()],
    sd: Annotated[int, Body()],
    pulse: Annotated[int, Body()],
    created_at: Annotated[datetime, Body()],
    comment: Annotated[str, Body()],
    status: Annotated[str, Body()],
):
    created = created_at.strftime('%Y-%m-%d %H:%M:%S')
    data = {
        'ad': ad,
        'sd': sd,
        'pulse': pulse,
        'created_at': created,
        'comment': comment,
        'status': status,
    }
    cache_key = f'tdn:measurement:{user}:{created}'
    cache.set(cache_key, dumps(data))
    return


@router.get('/measurements')
async def measurements(
    user: Annotated[str, Depends(login)],
):
    data = [cache.get(key) for key in cache.keys(f'tdn:measurement:{user}:*')]
    return data


@router.get('/queue')
async def queue(user: Annotated[bool, Depends(login)]):
    return {
        'id': 60,
        'guid': '92b3343d-1cb2-47b2-8497-a37e38b6ba24',
        'tmk_date': None,
        'created_at': '2025-04-02 15:21:19.890343',
        'code_mo': '166502',
        'mo_name': 'ГАУЗ "ГКБ№7 ИМ. М.Н.САДЫКОВА"',
        'doctor_spec': '109',
        'doctor_snils': None,
        'patient_name': 'Иванов Петр Федорович',
        'patient_birthday': '1997-03-01',
        'patient_snils': '099-678-666 12',
        'patient_policy': None,
        'patient_phone': '+79123456789',
        'patient_email': None,
        'tmk_status': 1,
        'tmk_status_name': 'Создана',
        'tmk_cancel_reason': None,
        'tmk_cancel_reason_name': None,
        'vks_doctor_link': None,
        'vks_patient_link': None,
        'doctor_spec_name': 'врач-терапевт',
    }


@router.get('/findPat')
async def find_pat(user: Annotated[str, Depends(login)]):
    return mock.findpat[0]


@router.get('/getELNS')
async def get_elns(user: Annotated[str, Depends(login)]):
    return mock.elns[0]


@router.get('/getDiagnosticResults')
async def get_diagnostic_results(user: Annotated[str, Depends(login)]):
    return mock.diagnosticResults


@router.get('/getPatFLG')
async def get_pat_flg(user: Annotated[str, Depends(login)]):
    return mock.patFLG[0]


@router.get('/getHospRoutes')
async def get_hosp_routes(user: Annotated[str, Depends(login)]):
    return mock.hospRoutes
