package go.kr.project.api.model;

import go.kr.project.api.model.request.NewLedgerRequest;
import go.kr.project.api.model.response.NewLedgerResponse;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 자동차 등록 원부(갑) 마스터 엔티티 VO, DB 저장용
 *
 * <p>tb_car_ledger_frmbk 테이블 매핑</p>
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CarLedgerFrmbkVO {
    // PK
    private String carLedgerFrmbkId;

    // 신규 응답용 txId
    private String txId;

    // 요청(헤더/본문) 정보 - DB 컬럼명과 동일하게
    private String infoSysId;           // INFO_SYS_ID
    private String infoSysIpAddr;       // INFO_SYS_IP_ADDR
    private String sggCd;               // SGG_CD
    private String linkInfoCd;          // LINK_INFO_CD
    private String picId;               // PIC_ID
    private String picIpAddr;           // PIC_IP_ADDR
    private String picNm;               // PIC_NM

    // 요청 본문 - DB 컬럼명과 동일하게
    private String dmndVhrno;           // DMND_VHRNO
    private String dmndPrvcRls;         // DMND_PRVC_RLS
    private String dmndCvlprNm;         // DMND_CVLPR_NM
    private String dmndCvlprIdecno;     // DMND_CVLPR_IDECNO
    private String dmndCvlprStdgCd;     // DMND_CVLPR_STDG_CD
    private String dmndPathSeCd;        // DMND_PATH_SE_CD
    private String dmndDsctnIndct;      // DMND_DSCTN_INDCT
    private String dmndInqSeCd;         // DMND_INQ_SE_CD

    // 응답 요약 정보 - DB 컬럼명과 동일하게
    private String linkRsltCd;          // LINK_RSLT_CD
    private String linkRsltDtl;         // LINK_RSLT_DTL

    // 응답 본문(마스터) - DB 컬럼명과 정확히 일치
    private String ledgerGroupNo;           // LEDGER_GROUP_NO
    private String ledgerIndivNo;           // LEDGER_INDIV_NO
    private String vhmno;                   // VHMNO
    private String vhrno;                   // VHRNO
    private String vin;                     // VIN
    private String carmdlAsortCd;           // CARMDL_ASORT_CD
    private String carmdlAsortNm;           // CARMDL_ASORT_NM
    private String atmbNm;                  // ATMB_NM
    private String colorCd;                 // COLOR_CD
    private String colorNm;                 // COLOR_NM
    private String nopltSpcfctCd;           // NOPLT_SPCFCT_CD
    private String nopltSpcfctNm;           // NOPLT_SPCFCT_NM
    private String usgSeCd;                 // USG_SE_CD
    private String usgSeNm;                 // USG_SE_NM
    private String mtrsFomNm;               // MTRS_FOM_NM
    private String fomNm;                   // FOM_NM
    private String acqsAmt;                 // ACQS_AMT
    private String regDtlCd;                // REG_DTL_CD
    private String regDtlNm;                // REG_DTL_NM
    private String frstRegYmd;              // FRST_REG_YMD
    private String veagEndYmd;              // VEAG_END_YMD
    private String yridnw;                  // YRIDNW
    private String spmnno1;                 // SPMNNO_1
    private String spmnno2;                 // SPMNNO_2
    private String fbctnYmd;                // FBCTN_YMD
    private String drvngDstnc;              // DRVNG_DSTNC
    private String inspVldPdBgngYmd;        // INSP_VLD_PD_BGNG_YMD
    private String inspVldPdEndYmd;         // INSP_VLD_PD_END_YMD
    private String chckVldPdBgngYmd;        // CHCK_VLD_PD_BGNG_YMD
    private String chckVldPdEndYmd;         // CHCK_VLD_PD_END_YMD
    private String regAplySeNm;             // REG_APLY_SE_NM
    private String frstRegAplyRcptNo;       // FRST_REG_APLY_RCPT_NO
    private String nopltCsdyAvtsmtYmd;      // NOPLT_CSDY_AVTSMT_YMD
    private String nopltCsdyYn;             // NOPLT_CSDY_YN
    private String bssUsePdYmd;             // BSS_USE_PD_YMD
    private String octhtErsrPrvntcAvtsmtYmd;// OCTHT_ERSR_PRVNTC_AVTSMT_YMD
    private String ersrRegYmd;              // ERSR_REG_YMD
    private String ersrRegSeCd;             // ERSR_REG_SE_CD
    private String ersrRegSeNm;             // ERSR_REG_SE_NM
    private String mrtgCnt;                 // MRTG_CNT
    private String szrCnt;                  // SZR_CNT
    private String strctChgCnt;             // STRCT_CHG_CNT
    private String usgsrhldAddr1;           // USGSRHLD_ADDR_1
    private String usgsrhldAddrDtl1;        // USGSRHLD_ADDR_DTL_1
    private String ownrAddr;                // OWNR_ADDR
    private String ownrAddrDtl;             // OWNR_ADDR_DTL
    private String indvdlBzmnYn;            // INDVDL_BZMN_YN
    private String rprsOwnrTelno;           // RPRS_OWNR_TELNO
    private String rprsOwnrNm;              // RPRS_OWNR_NM
    private String rprsOwnrMbrSeCd;         // RPRS_OWNR_MBR_SE_CD
    private String rprsvOwnrIdecno;         // RPRSV_OWNR_IDECNO
    private String taxxmptTrprSeCd;         // TAXXMPT_TRPR_SE_CD
    private String taxxmptAplcnSeCd;        // TAXXMPT_APLCN_SE_CD
    private String spcablMttrCnt;           // SPCABL_MTTR_CNT
    private String usgsrhldDongNm;          // USGSRHLD_DONG_NM
    private String prvntcCnt;               // PRVNTC_CNT
    private String xportFlflYnDclrYmd;      // XPORT_FLFL_YN_DCLR_YMD
    private String issuNo;                  // ISSU_NO
    private String frstTrnsfrYmd;           // FRST_TRNSFR_YMD
    private String drivSrgbtryIdntfNo;      // DRIV_SRGBTRY_IDNTF_NO
    private String prcsImprtyRsnCd;         // PRCS_IMPRTY_RSN_CD
    private String prcsImprtyRsnDtls;       // PRCS_IMPRTY_RSN_DTLS
    private String carFfnlgTrgtId;          // CAR_FFNLG_TRGT_ID

    // 감사
    private String rgtr;

    private List<CarLedgerFrmbkDtlVO> record;

    /**
     * NewLedgerRequest에서 VO로 변환 (최초 요청 로그 저장용)
     *
     * @param request NewLedgerRequest 요청 객체
     * @return 초기 요청 정보가 담긴 VO
     */
    public static CarLedgerFrmbkVO fromNewRequest(NewLedgerRequest request) {
        if (request == null) {
            return null;
        }

        return CarLedgerFrmbkVO.builder()
                .infoSysId(request.getInfoSysId())
                .infoSysIpAddr(request.getInfoSysIpAddr())
                .sggCd(request.getSggCd())
                .linkInfoCd(request.getLinkInfoCd())
                .picId(request.getPicId())
                .picIpAddr(request.getPicIpAddr())
                .picNm(request.getPicNm())
                .dmndVhrno(request.getVhrno())
                .dmndPrvcRls(request.getPrvcRls())
                .dmndCvlprNm(request.getCvlprNm())
                .dmndCvlprIdecno(request.getCvlprIdecno())
                .dmndCvlprStdgCd(request.getCvlprStdgCd())
                .dmndPathSeCd(request.getPathSeCd())
                .dmndDsctnIndct(request.getDsctnIndct())
                .dmndInqSeCd(request.getInqSeCd())
                .build();
    }

    /**
     * NewLedgerResponse에서 VO로 변환 (응답 마스터 정보 업데이트용)
     *
     * @param generatedId 생성된 PK ID
     * @param response NewLedgerResponse 응답 객체
     * @return 응답 마스터 정보가 담긴 VO
     */
    public static CarLedgerFrmbkVO fromNewResponseMaster(
            String generatedId,
            NewLedgerResponse response) {

        if (response == null) {
            return null;
        }

        return CarLedgerFrmbkVO.builder()
                .carLedgerFrmbkId(generatedId)
                .linkRsltCd(response.getLinkRsltCd())
                .linkRsltDtl(response.getLinkRsltDtl())
                .ledgerGroupNo(response.getLedgerGroupNo())
                .ledgerIndivNo(response.getLedgerIndivNo())
                .vhmno(response.getVhmno())
                .vhrno(response.getVhrno())
                .vin(response.getVin())
                .carmdlAsortCd(response.getCarmdlAsortCd())
                .carmdlAsortNm(response.getCarmdlAsortNm())
                .atmbNm(response.getAtmbNm())
                .colorCd(response.getColorCd())
                .colorNm(response.getColorNm())
                .nopltSpcfctCd(response.getNopltSpcfctCd())
                .nopltSpcfctNm(response.getNopltSpcfctNm())
                .usgSeCd(response.getUsgSeCd())
                .usgSeNm(response.getUsgSeNm())
                .mtrsFomNm(response.getMtrsFomNm())
                .fomNm(response.getFomNm())
                .acqsAmt(response.getAcqsAmt())
                .regDtlCd(response.getRegDtlCd())
                .regDtlNm(response.getRegDtlNm())
                .frstRegYmd(response.getFrstRegYmd())
                .veagEndYmd(response.getVeagEndYmd())
                .yridnw(response.getYridnw())
                .spmnno1(response.getSpmnno1())
                .spmnno2(response.getSpmnno2())
                .fbctnYmd(response.getFbctnYmd())
                .drvngDstnc(response.getDrvngDstnc())
                .inspVldPdBgngYmd(response.getInspVldPdBgngYmd())
                .inspVldPdEndYmd(response.getInspVldPdEndYmd())
                .chckVldPdBgngYmd(response.getChckVldPdBgngYmd())
                .chckVldPdEndYmd(response.getChckVldPdEndYmd())
                .regAplySeNm(response.getRegAplySeNm())
                .frstRegAplyRcptNo(response.getFrstRegAplyRcptNo())
                .nopltCsdyAvtsmtYmd(response.getNopltCsdyAvtsmtYmd())
                .nopltCsdyYn(response.getNopltCsdyYn())
                .bssUsePdYmd(response.getBssUsePdYmd())
                .octhtErsrPrvntcAvtsmtYmd(response.getOcthtErsrPrvntcAvtsmtYmd())
                .ersrRegYmd(response.getErsrRegYmd())
                .ersrRegSeCd(response.getErsrRegSeCd())
                .ersrRegSeNm(response.getErsrRegSeNm())
                .mrtgCnt(response.getMrtgCnt())
                .szrCnt(response.getSzrCnt())
                .strctChgCnt(response.getStrctChgCnt())
                .usgsrhldAddr1(response.getUsgsrhldAddr1())
                .usgsrhldAddrDtl1(response.getUsgsrhldAddrDtl1())
                .ownrAddr(response.getOwnrAddr())
                .ownrAddrDtl(response.getOwnrAddrDtl())
                .indvdlBzmnYn(response.getIndvdlBzmnYn())
                .rprsOwnrTelno(response.getRprsOwnrTelno())
                .rprsOwnrNm(response.getRprsOwnrNm())
                .rprsOwnrMbrSeCd(response.getRprsOwnrMbrSeCd())
                .rprsvOwnrIdecno(response.getRprsvOwnrIdecno())
                .taxxmptTrprSeCd(response.getTaxxmptTrprSeCd())
                .taxxmptAplcnSeCd(response.getTaxxmptAplcnSeCd())
                .spcablMttrCnt(response.getSpcablMttrCnt())
                .usgsrhldDongNm(response.getUsgsrhldDongNm())
                .prvntcCnt(response.getPrvntcCnt())
                .xportFlflYnDclrYmd(response.getXportFlflYnDclrYmd())
                .issuNo(response.getIssuNo())
                .frstTrnsfrYmd(response.getFrstTrnsfrYmd())
                .drivSrgbtryIdntfNo(response.getDrivSrgbtryIdntfNo())
                .prcsImprtyRsnCd(response.getProcessImprtyResnCode())
                .prcsImprtyRsnDtls(response.getProcessImprtyResnDtls())
                .build();
    }

    /**
     * NewLedgerRequest에서 필수 조회 항목 추출 (DB 조회용)
     *
     * @param request NewLedgerRequest 요청 객체
     * @return 차량번호
     */
    public static String extractVhrno(NewLedgerRequest request) {
        return request != null ? request.getVhrno() : null;
    }
}
