import html
from typing import Optional

from fastapi import Form

from src.model.request.request_base_model import RequestBaseModel


# @sanitize
class UltmarcInstSearchModel(RequestBaseModel):
    dcf_dsf_inst_cd: Optional[str]
    inst_div_cd: Optional[str]
    form_inst_name_kanji: Optional[str]
    form_inst_name_kana: Optional[str]
    postal_number: Optional[str]
    inst_phone_number: Optional[str]
    prefc_cd: Optional[str]
    delFlg: Optional[str]
    inst_addr: Optional[str]
    pagination_page_number: Optional[int]

    @classmethod
    def as_form(
        cls,
        ctrl_dcf_dsf_inst_cd: str = Form(None),
        ctrl_inst_div_cd: str = Form(None),
        ctrl_form_inst_name_kanji: str = Form(None),
        ctrl_form_inst_name_kana: str = Form(None),
        ctrl_postal_number: str = Form(None),
        ctrl_inst_phone_number: str = Form(None),
        ctrl_prefc_cd: str = Form(None),
        delFlg_ctrl: str = Form(None),
        ctrl_inst_addr: str = Form(None),
        pagination_page_number: str = Form(None)
    ):
        return cls(
            dcf_dsf_inst_cd=ctrl_dcf_dsf_inst_cd,
            inst_div_cd=ctrl_inst_div_cd,
            form_inst_name_kanji=ctrl_form_inst_name_kanji,
            form_inst_name_kana=ctrl_form_inst_name_kana,
            postal_number=ctrl_postal_number,
            inst_phone_number=ctrl_inst_phone_number,
            prefc_cd=ctrl_prefc_cd,
            delFlg=delFlg_ctrl,
            inst_addr=ctrl_inst_addr,
            pagination_page_number=pagination_page_number
        )

    def unescape(self):
        for k, v in self.dict().items():
            if v is not None and type(v) is str:
                setattr(self, k, html.unescape(v))
        return self


class UltmarcInstInfoModel(RequestBaseModel):
    inst_id: Optional[str]
    page_num: Optional[int]

    @classmethod
    def as_form(
        cls,
        inst_id: str = Form(None),
        page_num: str = Form(None)
    ):

        return cls(
            inst_id=inst_id,
            page_num=int(page_num)
        )
