package com.oqpo.api.web.dto.response.inoutmng;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.inoutmng.DeliDtlMgntEntity;
import com.oqpo.api.entity.inoutmng.DeliInfoMgntEntity;
import com.oqpo.api.entity.inoutmng.PickDtlPtclEntity;
import com.oqpo.api.entity.inoutmng.PickInfoMgntEntity;
import com.oqpo.api.entity.oper.FileEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.util.List;
import java.util.stream.Collectors;


@Getter
@Setter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@JsonInclude(JsonInclude.Include.ALWAYS)
public class ColInfoResponse {

    @ApiModelProperty(value = "발행일") private String colRegDt;
    @ApiModelProperty(value = "수거서번호") private String colPickUnqNo;
    @ApiModelProperty(value = "수거일") private String colSchDt;
    @ApiModelProperty(value = "수거담당자") private String pickMgrNm;  // +  setPickMgrTelNo
    @ApiModelProperty(value = "수거장소") private String whsAddr;
    @ApiModelProperty(value = "수거전화번호") private String pndrTelNo;
    @ApiModelProperty(value = "수거처-등록번호") private String colBsnsRegno;
    @ApiModelProperty(value = "수거처-상호") private String colNm;
    @ApiModelProperty(value = "수거처-성명") private String colOwnerNm;
    @ApiModelProperty(value = "수거처-주소") private String colAddr;
    @ApiModelProperty(value = "반품처-등록번호") private String rtnBsnsRegno;
    @ApiModelProperty(value = "반품처-상호") private String rtnNm;
    @ApiModelProperty(value = "반품처-성명") private String rtnOwnerNm;
    @ApiModelProperty(value = "반품처-주소") private String rtnAddr;


    @ApiModelProperty(value = "수거품목 정보")
    private List<PickDtlInfo> pickDtlInfoList;

    @ApiModelProperty(value = "로그파일 정보")
    private List<FileLogInfo> fileLogNoList;

    public static ColInfoResponse toDTO(PickInfoMgntEntity entity, List<PickDtlPtclEntity>  dtlList, List<FileEntity>  logFileList) {
        if (entity == null) return null;
        return ColInfoResponse.builder()
                .colRegDt(entity.getColRegDt() == null ? "" : entity.getColRegDt())
                .colPickUnqNo(entity.getColPickUnqNo() == null ? "" : entity.getColPickUnqNo())
                .colSchDt(entity.getColSchDt() == null ? "" : entity.getColSchDt())
                .pickMgrNm(entity.getPickMgrNm() == null ? "" : entity.getPickMgrNm() + "(" + entity.getPickMgrTelNo() + ")")
                .whsAddr(entity.getRtnAddr() == null ? "" : entity.getRtnAddr())
                .pndrTelNo(entity.getPndrTelNo() == null ? "" : entity.getPndrTelNo())
                .colBsnsRegno(entity.getColBsnsRegno() == null ? "" : entity.getColBsnsRegno())
                .colNm(entity.getColNm() == null ? "" : entity.getColNm())
                .colOwnerNm(entity.getColOwnerNm() == null ? "" : entity.getColOwnerNm())
                .colAddr(entity.getColAddr() == null ? "" : entity.getColAddr())
                .rtnBsnsRegno(entity.getRtnBsnsRegno() == null ? "" : entity.getRtnBsnsRegno())
                .rtnNm(entity.getRtnNm() == null ? "" : entity.getRtnNm())
                .rtnOwnerNm(entity.getRtnOwnerNm() == null ? "" : entity.getRtnOwnerNm())
                .rtnAddr(entity.getRtnAddr() == null ? "" : entity.getRtnAddr())
                .fileLogNoList(logFileList.stream()
                        .map(m -> ColInfoResponse.FileLogInfo.builder()
                                .fileNo(m.getFileNo())
                                .fileSeq(m.getFileSeq())
                                .filePath(m.getFilePath())
                                .fileNm(m.getFileNm())
                                .fileOrgNm(m.getFileOrgNm())
                                .build())
                        .collect(Collectors.toList()))
                .pickDtlInfoList(dtlList.stream()
                        .map(m -> ColInfoResponse.PickDtlInfo.builder()
                                .rtnPodrUnqNo(m.getRtnPodrUnqNo() == null ? "" : m.getRtnPodrUnqNo())
                                .itemId(m.getItemId() == null ? "" : m.getItemId())
                                .itemNm(m.getItemNm() == null ? "" : m.getItemNm())
                                .rodrQty(m.getRodrQty() == null ? 0 : m.getRodrQty())
                                .colReqQty(m.getColReqQty() == null ? 0 : m.getColReqQty())
                                .rtnRsnDvsnNm(m.getRtnRsnDvsnNm() == null ? "" : m.getRtnRsnDvsnNm())
                                .rtnRsn(m.getRtnRsn() == null ? "" : m.getRtnRsn())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class PickDtlInfo {
        @ApiModelProperty(value = "반품발주번호") private String rtnPodrUnqNo;
        @ApiModelProperty(value = "품목아이디") private String itemId;
        @ApiModelProperty(value = "품목명") private String itemNm;
        @ApiModelProperty(value = "반품수량") private Integer rodrQty;
        @ApiModelProperty(value = "수거수량") private Integer colReqQty;
        @ApiModelProperty(value = "반품사유구분") private String rtnRsnDvsnNm;
        @ApiModelProperty(value = "반품사유") private String rtnRsn;
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class FileLogInfo {
        @ApiModelProperty(value = "파일번호")
        private String fileNo;

        @ApiModelProperty(value = "파일순번")
        private Integer fileSeq;

        @ApiModelProperty(value = "파일경로")
        private String filePath;

        @ApiModelProperty(value = "파일명")
        private String fileNm;

        @ApiModelProperty(value = "원파일명")
        private String fileOrgNm;
    }
}
