from src.aws.aws_api_client import AWSAPIClient
from src.model.request.ultmarc_doctor import UltmarcDoctorSearchModel
from src.model.request.ultmarc_inst import UltmarcInstSearchModel
from src.model.view.ultmarc_doctor_info_view_model import \
    UltmarcDoctorInfoViewModel
from src.model.view.ultmarc_doctor_view_model import UltmarcDoctorViewModel
from src.model.view.ultmarc_inst_info_view_model import \
    UltmarcInstInfoViewModel
from src.model.view.ultmarc_inst_view_model import UltmarcInstViewModel
from src.repositories.base_repository import BaseRepository
from src.repositories.inst_master_repository import InstDivMasterRepository
from src.repositories.prefc_master_repository import PrefcMasterRepository
from src.repositories.ultmarc_doctor_repository import UltmarcDoctorRepository
from src.repositories.ultmarc_dr_wrkplace_his_repository import \
    UltmarcDoctorWrkplaceHisRepository
from src.repositories.ultmarc_dr_wrkplace_repository import \
    UltmarcDoctorWrkplaceRepository
from src.repositories.ultmarc_inst_repository import UltmarcInstRepository
from src.repositories.ultmarc_inst_trt_course_repository import \
    UltmarcInstTrtCourseRepository
from src.repositories.ultmarc_sosiety_repository import \
    UltmarcSosietyRepository
from src.repositories.ultmarc_specialist_license_repository import \
    UltmarcSpecialistLicenseRepository
from src.repositories.ultmarc_trt_course_repository import \
    UltmarcDrTrtCourseRepository
from src.services.base_service import BaseService


class UltmarcViewService(BaseService):
    REPOSITORIES = {
        'ultmarc_doctor_repository': UltmarcDoctorRepository,
        'prefc_repository': PrefcMasterRepository,
        'inst_div_repository': InstDivMasterRepository,
        'ultmarc_inst_repository': UltmarcInstRepository,
        'ultmarc_trt_course_repository': UltmarcDrTrtCourseRepository,
        'ultmarc_inst_trt_course_repository': UltmarcInstTrtCourseRepository,
        'ultmarc_sosiety_repository': UltmarcSosietyRepository,
        'ultmarc_doctor_wrkplace_repository': UltmarcDoctorWrkplaceRepository,
        'ultmarc_doctor_wrkplace_his_repository': UltmarcDoctorWrkplaceHisRepository,
        'ultmarc_specialist_license_repository': UltmarcSpecialistLicenseRepository
    }

    ultmarc_doctor_repository: UltmarcDoctorRepository
    prefc_repository: PrefcMasterRepository
    inst_div_repository: InstDivMasterRepository
    ultmarc_inst_repository: UltmarcInstRepository
    ultmarc_trt_course_repository: UltmarcDrTrtCourseRepository
    ultmarc_inst_trt_course_repository: UltmarcInstTrtCourseRepository
    ultmarc_sosiety_repository: UltmarcSosietyRepository
    ultmarc_doctor_wrkplace_repository: UltmarcDoctorWrkplaceRepository
    ultmarc_doctor_wrkplace_his_repository: UltmarcDoctorWrkplaceHisRepository
    ultmarc_specialist_license_repository: UltmarcSpecialistLicenseRepository

    def __init__(self, repositories: dict[str, BaseRepository], clients: dict[str, AWSAPIClient]) -> None:
        super().__init__(repositories, clients)
        self.ultmarc_doctor_repository = repositories['ultmarc_doctor_repository']
        self.prefc_repository = repositories['prefc_repository']
        self.inst_div_repository = repositories['inst_div_repository']
        self.ultmarc_inst_repository = repositories['ultmarc_inst_repository']
        self.ultmarc_trt_course_repository = repositories['ultmarc_trt_course_repository']
        self.ultmarc_inst_trt_course_repository = repositories['ultmarc_inst_trt_course_repository']
        self.ultmarc_sosiety_repository = repositories['ultmarc_sosiety_repository']
        self.ultmarc_doctor_wrkplace_repository = repositories['ultmarc_doctor_wrkplace_repository']
        self.ultmarc_doctor_wrkplace_his_repository = repositories['ultmarc_doctor_wrkplace_his_repository']
        self.ultmarc_specialist_license_repository = repositories['ultmarc_specialist_license_repository']

    #########################
    # アルトマーク医師       #
    #########################
    def prepare_ultmarc_doctor_search_view(
        self
    ) -> UltmarcDoctorViewModel:
        # 医師一覧画面の表示データ取得
        # 都道府県リストを取得
        prefcs = self.prefc_repository.fetch_all()

        ultmarc = UltmarcDoctorViewModel(
            prefc_models=prefcs
        )
        return ultmarc

    def search_doctor_data(self, search_params: UltmarcDoctorSearchModel):
        # 医師データを検索
        ultmarc_doctor_data = self.ultmarc_doctor_repository.fetch_many(parameter=search_params)
        return ultmarc_doctor_data

    def prepare_ultmarc_doctor_info_view(
        self,
        id
    ) -> UltmarcDoctorInfoViewModel:
        # 医師情報画面の表示データ取得
        # 医師情報を取得
        doctor_info = self.ultmarc_doctor_repository.fetch_one(id)
        # 診療科目情報を取得
        trt_course = self.ultmarc_trt_course_repository.fetch_many(id)
        # 所属学会情報を取得
        sosiety = self.ultmarc_sosiety_repository.fetch_many(id)
        # 所属学会専門医情報を取得
        specialist_license = self.ultmarc_specialist_license_repository.fetch_many(id)
        # 勤務先情報を取得
        wrkplace = self.ultmarc_doctor_wrkplace_repository.fetch_many(id)
        # 勤務先履歴情報を取得
        wrkplace_his = self.ultmarc_doctor_wrkplace_his_repository.fetch_many(id)

        ultmarc = UltmarcDoctorInfoViewModel(
            doctor_info_data=doctor_info,
            trt_coursed_data=trt_course,
            sosiety_data=sosiety,
            specialist_license_data=specialist_license,
            doctor_wrkplace_data=wrkplace,
            doctor_wrkplace_his_data=wrkplace_his
        )
        return ultmarc

    #########################
    # アルトマーク施設       #
    #########################
    def prepare_ultmarc_inst_search_view(
        self
    ) -> UltmarcInstViewModel:
        # 施設一覧画面の表示データ取得
        # 都道府県リストを取得
        prefcs = self.prefc_repository.fetch_all()
        # 施設区分リストを取得
        inst_div = self.inst_div_repository.fetch_all()

        ultmarc = UltmarcInstViewModel(
            prefc_models=prefcs,
            inst_div_models=inst_div
        )
        return ultmarc

    def search_inst_data(self, search_params: UltmarcInstSearchModel):
        # 施設データを検索
        ultmarc_inst_data = self.ultmarc_inst_repository.fetch_many(parameter=search_params)
        return ultmarc_inst_data

    def prepare_ultmarc_inst_info_view(
        self,
        id
    ) -> UltmarcInstInfoViewModel:
        # 施設情報画面の表示データ取得
        # 施設情報を取得
        inst_info = self.ultmarc_inst_repository.fetch_one(id)
        # 診療科目情報を取得
        inst_trt_course = self.ultmarc_inst_trt_course_repository.fetch_many(id)
        # 医師件数を取得
        doctor_count = self.ultmarc_doctor_wrkplace_repository.fetch_count(id)

        ultmarc = UltmarcInstInfoViewModel(
            inst_info_data=inst_info,
            inst_trt_coursed_data=inst_trt_course,
            doctor_wrkplace_count=doctor_count
        )
        return ultmarc
