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.loanmng.LoanMgntBaseInfoEntity;
import com.oqpo.api.entity.oper.FileEntity;
import com.oqpo.api.entity.oper.UserMngEntity;
import com.oqpo.api.entity.pomng.PchReqBaseInfoEntity;
import com.oqpo.api.entity.pomng.PchReqDtlPtclEntity;
import com.oqpo.api.web.dto.response.stinfo.brand.BrandInfoResponse;
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 InvInfoResponse {

    @ApiModelProperty(value = "발행일") private String dlvRegDt;
    @ApiModelProperty(value = "납품서번호") private String dlvSttmtUnqNo;
    @ApiModelProperty(value = "납품일") private String dvlSchDt;
    @ApiModelProperty(value = "납품담당자") private String dlvMgrNm;  // dlv_mgr_tel_no
    @ApiModelProperty(value = "납품장소") private String whsAddr;
    @ApiModelProperty(value = "납품전화번호") private String whsMgrTelNo;
    @ApiModelProperty(value = "공급-등록번호") private String dlvBsnsRegno;
    @ApiModelProperty(value = "공급-상호") private String dlvNm;
    @ApiModelProperty(value = "공급-성명") private String dlvOwnerNm;
    @ApiModelProperty(value = "공급-주소") private String dlvAddr;
    @ApiModelProperty(value = "발주-등록번호") private String poBsnsRegno;
    @ApiModelProperty(value = "발주-상호") private String poNm;
    @ApiModelProperty(value = "발주-성명") private String poOwnerNm;
    @ApiModelProperty(value = "발주-주소") private String poAddr;


    @ApiModelProperty(value = "납품 품목 정보")
    private List<InvDtlInfo> invDtlInfoList;

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

    public static InvInfoResponse toDTO(DeliInfoMgntEntity entity, List<DeliDtlMgntEntity>  dtlList, List<FileEntity>  logFileList) {
        if (entity == null) return null;
        return InvInfoResponse.builder()
                .dlvRegDt(entity.getDlvRegDt() == null ? "" : entity.getDlvRegDt())
                .dlvSttmtUnqNo(entity.getDlvSttmtUnqNo() == null ? "" : entity.getDlvSttmtUnqNo())
                .dvlSchDt(entity.getDlvSchDt() == null ? "" :  entity.getDlvSchDt())
                .dlvMgrNm(entity.getDlvMgrNm() == null ? "" : entity.getDlvMgrNm() + "( " + entity.getDlvMgrTelNo() + " )")
                .whsAddr(entity.getWhsAddr() == null ? "" : entity.getWhsAddr())
                .whsMgrTelNo(entity.getWhsMgrTelNo() == null ? "" : entity.getWhsMgrTelNo())
                .dlvBsnsRegno(entity.getDlvBsnsRegno() == null ? "" : entity.getDlvBsnsRegno())
                .dlvNm(entity.getDlvNm() == null ? "" : entity.getDlvNm())
                .dlvOwnerNm(entity.getDlvOwnerNm() == null ? "" : entity.getDlvOwnerNm())
                .dlvAddr(entity.getDlvAddr() == null ? "" : entity.getDlvAddr())
                .poBsnsRegno(entity.getPoBsnsRegno() == null ? "" : entity.getPoBsnsRegno())
                .poNm(entity.getPoNm() == null ? "" : entity.getPoNm())
                .poOwnerNm(entity.getPoOwnerNm() == null ? "" : entity.getPoOwnerNm())
                .poAddr(entity.getPoAddr() == null ? "" : entity.getPoAddr())
                .fileLogNoList(logFileList.stream()
                        .map(m -> InvInfoResponse.FileLogInfo.builder()
                                .fileNo(m.getFileNo())
                                .fileSeq(m.getFileSeq())
                                .filePath(m.getFilePath())
                                .fileNm(m.getFileNm())
                                .fileOrgNm(m.getFileOrgNm())
                                .build())
                        .collect(Collectors.toList()))
                .invDtlInfoList(dtlList.stream()
                        .map(m -> InvInfoResponse.InvDtlInfo.builder()
                                .pchPodrUnqNo(m.getPchPodrUnqNo() == null ? "" : m.getPchPodrUnqNo())
                                .itemId(m.getItemId() == null ? "" :m.getItemId())
                                .itemNm(m.getItemNm() == null ? "" :m.getItemNm())
                                .podrQty(m.getPodrQty() == null ? 0 :m.getPodrQty())
                                .dlvQty(m.getDlvQty() == null ? 0 :m.getDlvQty())
                                .expryDate(m.getExpryDate() == null ? "" : m.getExpryDate())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class InvDtlInfo {
        @ApiModelProperty(value = "구매발주번호") private String pchPodrUnqNo;
        @ApiModelProperty(value = "품목아이디") private String itemId;
        @ApiModelProperty(value = "품목명") private String itemNm;
        @ApiModelProperty(value = "발주수량") private Integer podrQty;
        @ApiModelProperty(value = "납품수량") private Integer dlvQty;
        @ApiModelProperty(value = "유통기간") private String expryDate;
    }

    @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;
    }
}
