from typing import Optional

from fastapi import Body, Form
from pydantic import BaseModel

from src.util.sanitize import sanitize
from src.util.string_util import is_not_empty


@sanitize
class BioModel(BaseModel):
    rec_whs_cd: Optional[str]
    rec_whs_sub_cd: Optional[str]
    whs_name: Optional[str]
    slip_org_kbn: Optional[str]
    rec_ymd_from: Optional[str]
    rec_ymd_to: Optional[str]
    rec_lot_num: Optional[str]
    data_kbn: Optional[str]
    mkr_cd: Optional[str]
    rev_hsdnymd_srk_from: Optional[str]
    rev_hsdnymd_srk_to: Optional[str]
    iko_flg: Optional[str]

    @classmethod
    def as_form(
        cls,
        ctrl_wholesaler: str = Form(None),
        ctrl_org_kbn: str = Form(None),
        ctrl_rec_ymd_from: str = Form(None),
        ctrl_rec_ymd_to: str = Form(None),
        ctrl_rec_lot_num: str = Form(None),
        ctrl_data_kbn: str = Form(None),
        ctrl_maker_cd: str = Form(None),
        ctrl_rev_hsdnymd_srk_from: str = Form(None),
        ctrl_rev_hsdnymd_srk_to: str = Form(None),
        ikoFlg: str = Form(None)
    ):

        return cls.__convert_request_param(
            cls,
            ctrl_wholesaler,
            ctrl_org_kbn,
            ctrl_rec_ymd_from,
            ctrl_rec_ymd_to,
            ctrl_rec_lot_num,
            ctrl_data_kbn,
            ctrl_maker_cd,
            ctrl_rev_hsdnymd_srk_from,
            ctrl_rev_hsdnymd_srk_to,
            ikoFlg
        )

    @classmethod
    def as_body(
        cls,
        ctrl_wholesaler: str = Body(None),
        ctrl_org_kbn: str = Body(None),
        ctrl_rec_ymd_from: str = Body(None),
        ctrl_rec_ymd_to: str = Body(None),
        ctrl_rec_lot_num: str = Body(None),
        ctrl_data_kbn: str = Body(None),
        ctrl_maker_cd: str = Body(None),
        ctrl_rev_hsdnymd_srk_from: str = Body(None),
        ctrl_rev_hsdnymd_srk_to: str = Body(None),
        ikoFlg: str = Body(None)
    ):

        return cls.__convert_request_param(
            cls,
            ctrl_wholesaler,
            ctrl_org_kbn,
            ctrl_rec_ymd_from,
            ctrl_rec_ymd_to,
            ctrl_rec_lot_num,
            ctrl_data_kbn,
            ctrl_maker_cd,
            ctrl_rev_hsdnymd_srk_from,
            ctrl_rev_hsdnymd_srk_to,
            ikoFlg
        )

    def __convert_request_param(
        cls,
        ctrl_wholesaler: str,
        ctrl_org_kbn: str,
        ctrl_rec_ymd_from: str,
        ctrl_rec_ymd_to: str,
        ctrl_rec_lot_num: str,
        ctrl_data_kbn: str,
        ctrl_maker_cd: str,
        ctrl_rev_hsdnymd_srk_from: str,
        ctrl_rev_hsdnymd_srk_to: str,
        ikoFlg: str
    ):
        wholesaler_code = None
        wholesaler_sub_code = None
        wholesaler_name = None
        # 卸コード・卸サブコード
        if is_not_empty(ctrl_wholesaler):
            # 卸コードは`020-01:卸名`という感じのデータで来るので、分割
            wholesaler_without_name = ctrl_wholesaler.split(':')[0]
            wholesaler_name = ctrl_wholesaler.split(':')[1]
            wholesaler_code = wholesaler_without_name.split('-')[0]
            wholesaler_sub_code = wholesaler_without_name.split('-')[1]

        # 処理日
        rec_ymd_from = None
        rec_ymd_to = None
        if is_not_empty(ctrl_rec_ymd_from):
            rec_ymd_from = ctrl_rec_ymd_from.replace('/', '')
        if is_not_empty(ctrl_rec_ymd_to):
            rec_ymd_to = ctrl_rec_ymd_to.replace('/', '')

        # 発伝年月日
        rev_hsdnymd_srk_from = None
        rev_hsdnymd_srk_to = None
        if is_not_empty(ctrl_rev_hsdnymd_srk_from):
            rev_hsdnymd_srk_from = ctrl_rev_hsdnymd_srk_from.replace('/', '')
        if is_not_empty(ctrl_rev_hsdnymd_srk_to):
            rev_hsdnymd_srk_to = ctrl_rev_hsdnymd_srk_to.replace('/', '')

        return cls(
            rec_whs_cd=wholesaler_code,
            rec_whs_sub_cd=wholesaler_sub_code,
            whs_name=wholesaler_name,
            slip_org_kbn=ctrl_org_kbn,
            rec_ymd_from=rec_ymd_from,
            rec_ymd_to=rec_ymd_to,
            rec_lot_num=ctrl_rec_lot_num,
            data_kbn=ctrl_data_kbn,
            mkr_cd=ctrl_maker_cd,
            rev_hsdnymd_srk_from=rev_hsdnymd_srk_from,
            rev_hsdnymd_srk_to=rev_hsdnymd_srk_to,
            iko_flg=ikoFlg
        )
