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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 자동차 등록 원부(갑) 상세 엔티티 VO, DB 저장용
 *
 * <p>tb_car_ledger_frmbk_dtl 테이블 매핑</p>
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CarLedgerFrmbkDtlVO {

    // PK
    private String carLedgerFrmbkDtlId;

    // FK
    private String carLedgerFrmbkId;        // CAR_LEDGER_FRMBK_ID

    // 본문 - DB 컬럼명과 정확히 일치
    private String szrRmvDtlSn;             // SZR_RMV_DTL_SN
    private String chgTaskSeCd;             // CHG_TASK_SE_CD
    private String mainNo;                  // MAIN_NO
    private String sno;                     // SNO
    private String spcablMttr;              // SPCABL_MTTR
    private String hshldrNm;                // HSHLDR_NM
    private String hshldrIdecno;            // HSHLDR_IDECNO
    private String aplyRcptNo;              // APLY_RCPT_NO
    private String vhmno;                   // VHMNO
    private String ledgerGroupNo;           // LEDGER_GROUP_NO
    private String ledgerIndivNo;           // LEDGER_INDIV_NO
    private String chgTaskSeNm;             // CHG_TASK_SE_NM
    private String chgYmd;                  // CHG_YMD
    private String dtlSn;                   // DTL_SN
    private String flag;                    // FLAG

    // 감사
    private String rgtr;                    // RGTR

    /**
     * ApiExchangeEnvelope에서 상세 VO 리스트로 변환
     *
     * @param envelope API 응답 Envelope
     * @param generatedId 생성된 마스터 PK ID
     * @return 상세 VO 리스트
     */
    public static List<CarLedgerFrmbkDtlVO> listNewFromExchange(
            ApiExchangeEnvelope<NewLedgerRequest, NewLedgerResponse> envelope,
            String generatedId) {

        if (envelope == null || envelope.getResponse() == null) {
            return new ArrayList<>();
        }

        Envelope<NewLedgerResponse> response = envelope.getResponse();
        List<NewLedgerResponse> data = response.getData();

        if (data == null || data.isEmpty()) {
            return new ArrayList<>();
        }

        NewLedgerResponse firstData = data.get(0);
        List<NewLedgerResponse.Record> records = firstData.getRecord();

        if (records == null || records.isEmpty()) {
            return new ArrayList<>();
        }

        // Record 리스트를 VmisCarLedgerFrmbkDtlVO 리스트로 변환
        return records.stream()
                .map(record -> CarLedgerFrmbkDtlVO.builder()
                        .carLedgerFrmbkId(generatedId)
                        .szrRmvDtlSn(record.getSzrRmvDtlSn())
                        .chgTaskSeCd(record.getChgTaskSeCd())
                        .mainNo(record.getMainNo())
                        .sno(record.getSno())
                        .spcablMttr(record.getSpcablMttr())
                        .hshldrNm(record.getHshldrNm())
                        .hshldrIdecno(record.getHshldrIdecno())
                        .aplyRcptNo(record.getAplyRcptNo())
                        .vhmno(record.getVhmno())
                        .ledgerGroupNo(record.getLedgerGroupNo())
                        .ledgerIndivNo(record.getLedgerIndivNo())
                        .chgTaskSeNm(record.getChgTaskSeNm())
                        .chgYmd(record.getChgYmd())
                        .dtlSn(record.getDtlSn())
                        .flag(record.getFlag())
                        .build())
                .collect(Collectors.toList());
    }

    /**
     * NewLedgerResponse.Record에서 단일 상세 VO로 변환
     *
     * @param record NewLedgerResponse.Record 객체
     * @param carLedgerFrmbkId 마스터 PK ID
     * @return 상세 VO
     */
    public static CarLedgerFrmbkDtlVO fromRecord(
            NewLedgerResponse.Record record,
            String carLedgerFrmbkId) {

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

        return CarLedgerFrmbkDtlVO.builder()
                .carLedgerFrmbkId(carLedgerFrmbkId)
                .szrRmvDtlSn(record.getSzrRmvDtlSn())
                .chgTaskSeCd(record.getChgTaskSeCd())
                .mainNo(record.getMainNo())
                .sno(record.getSno())
                .spcablMttr(record.getSpcablMttr())
                .hshldrNm(record.getHshldrNm())
                .hshldrIdecno(record.getHshldrIdecno())
                .aplyRcptNo(record.getAplyRcptNo())
                .vhmno(record.getVhmno())
                .ledgerGroupNo(record.getLedgerGroupNo())
                .ledgerIndivNo(record.getLedgerIndivNo())
                .chgTaskSeNm(record.getChgTaskSeNm())
                .chgYmd(record.getChgYmd())
                .dtlSn(record.getDtlSn())
                .flag(record.getFlag())
                .build();
    }
}
