
from src.db.database import Database
from src.batch.ultmarc.utmp_tables.table_mapper.ultmarc_table_mapper import \
    UltmarcTableMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_alma_mapper import \
    ComAlmaMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_dr_wrkplace_mapper import \
    ComDrWrkplaceMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_forfront_med_equip_mapper import \
    ComForfrontMedEquipMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_hamtec_mapper import \
    ComHamtecMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_inst_mapper import \
    ComInstMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_nurse_assrt_mapper import \
    ComNurseAssrtMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_pharm_mapper import \
    ComPharmMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_sp_field_mapper import \
    ComSpFieldMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_policy_med_mapper import \
    ComPolicyMedMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_rehabili_mapper import \
    ComRehabiliMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_regn_critic_pass_mapper import \
    ComRegnCriticPassMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_med_func_valuation_mapper import \
    ComMedFuncValuationMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_dr_mapper import \
    ComDrMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_dr_sosiety_mapper import \
    ComDrSosietyMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_prefc_med_base_mapper import \
    ComPrefcMedBaseMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_prefc_med_equpment_mapper import \
    ComPrefcMedEqupmentMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_prefc_med_dis_treat_mapper import \
    ComPrefcMedDisTreatMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_prefc_med_sojourn_ope_mapper import \
    ComPrefcMedSojournOpeMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_prefc_med_sp_outpat_mapper import \
    ComPrefcMedSpOutpatMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_inst_att_mapper import \
    ComInstAttMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_cop_hp_mapper import \
    ComCopHpMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_trt_course_mapper import \
    ComTrtCourseMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_hp_assrt_mapper import \
    ComHpAssrtMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_alma_depart_disc_mapper import \
    ComAlmaDepartDiscMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_med_prefc_mapper import \
    ComMedPrefcMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_thrd_med_mapper import \
    ComThrdMedMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_snd_med_sphe_mapper import \
    ComSndMedSpheMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_med_area_city_mapper import \
    ComMedAreaCityMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_post_mapper import \
    ComPostMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_manage_mapper import \
    ComManageMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.mst_prefc_mapper import \
    MstPrefcMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_blng_sec_mapper import \
    ComBlngSecMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_sosiety_mapper import \
    ComSosietyMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_specialist_license_mapper import \
    ComSpecialistLicenseMapper
from src.batch.ultmarc.utmp_tables.table_mapper.concrete.com_inst_div_mapper import \
    ComInstDivMapper

# テーブルとのマッピング
COM_TABLE_LIST = {
    # レコードID固定
    # COM_医師学会
    "521": ComDrSosietyMapper,
    # COM_施設属性
    "111": ComInstAttMapper,
    # COM_臨床研修病院
    "112": ComCopHpMapper,
    # COM_医師
    "501": ComDrMapper,
    # COM_施設
    "101": ComInstMapper,
    # COM_薬局
    "102": ComPharmMapper,
    # COM_医師勤務先
    "502": ComDrWrkplaceMapper,
    # COM_専門分野
    "511": ComSpFieldMapper,
    # COM_都道府県医療機能情報(基本)
    "132": ComPrefcMedBaseMapper,
    # COM_都道府県医療機能情報(施設設備)
    "133": ComPrefcMedEqupmentMapper,
    # COM_都道府県医療機能情報(疾患治療)
    "134": ComPrefcMedDisTreatMapper,
    # COM_都道府県医療機能情報(短期滞在手術)
    "135": ComPrefcMedSojournOpeMapper,
    # COM_都道府県医療機能情報(専門外来)
    "136": ComPrefcMedSpOutpatMapper,
    # COM_診療科目
    "001": ComTrtCourseMapper,
    # COM_病院種別
    "002": ComHpAssrtMapper,
    # COM_出身校学部識別
    "003": ComAlmaDepartDiscMapper,
    # COM_出身校
    "004": ComAlmaMapper,
    # COM_役職
    "005": ComPostMapper,
    # 都道府県マスタ
    "006": MstPrefcMapper,
    # COM_経営体
    "007": ComManageMapper,
    # COM_所属部科
    "008": ComBlngSecMapper,
    # COM_学会
    "009": ComSosietyMapper,
    # COM_専門医資格
    "010": ComSpecialistLicenseMapper,
    # COM_施設区分
    "011": ComInstDivMapper,
    # COM_高度先進医療
    "021": ComHamtecMapper,
    # COM_先端医療機器
    "022": ComForfrontMedEquipMapper,
    # COM_看護種別
    "023": ComNurseAssrtMapper,
    # COM_医療機能評価
    "024": ComMedFuncValuationMapper,
    # COM_地域クリティカルパス
    "026": ComRegnCriticPassMapper,
    # COM_疾患別リハビリテーション科
    "027": ComRehabiliMapper,
    # COM_政策医療
    "028": ComPolicyMedMapper,
    # COM_医療圏都道府県
    "121": ComMedPrefcMapper,
    # COM_医療圏３次マスタ
    "122": ComThrdMedMapper,
    # COM_二次医療圏
    "123": ComSndMedSpheMapper,
    # COM_医療圏都道府県市町村対照表
    "124": ComMedAreaCityMapper
}


class UltmarcTableMapperFactory:

    def create(self, layout_class: str, records: list[str], db: Database) -> UltmarcTableMapper:
        """レイアウト区分とレコードIDから、マッピング先のテーブルマッパーを特定する

        Args:
            layout_class (str): レイアウト区分
            records (list[str]): アルトマークデータの1行
            db (Database): データベース操作クラス

        Raises:
            Exception: レイアウトを特定できない場合

        Returns:
            UltmarcTableMapper: マッパークラス
        """
        # レイアウト区分から、マッピング先のテーブルを特定
        mapper_class = COM_TABLE_LIST.get(layout_class)
        # レイアウト区分が特定できない場合はエラーとする
        if mapper_class is None:
            raise Exception(f'マッピング先のテーブルを特定できませんでした。レイアウト区分={layout_class}')

        return mapper_class(records, db)
