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.oper.FileEntity;
import com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity;
import com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity;
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 SpplyInvInitResponse {

    @ApiModelProperty(value = "브랜드아이디") private String brandId;
    @ApiModelProperty(value = "브랜드명") private String brandNm;
    @ApiModelProperty(value = "납품서생성일") private String dlvReqDt;
    @ApiModelProperty(value = "납품총금액") private Long podrTotalAmt;  // dlv_mgr_tel_no
    @ApiModelProperty(value = "납품창고아이디") private String whsId;
    @ApiModelProperty(value = "납품찿고명") private String whsNm;
    @ApiModelProperty(value = "location") private String location;
    @ApiModelProperty(value = "납품장소전화번호") private String deliTelNo;


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

    public static SpplyInvInitResponse toDTO(PchOdrBaseInfoEntity entity, List<PchOdrDtlPtclEntity>  dtlList) {
        if (entity == null) return null;
        return SpplyInvInitResponse.builder()
                .brandId(entity.getBrandId() == null ? "" : entity.getBrandId())
                .brandNm(entity.getBrandNm() == null ? "" : entity.getBrandNm())
                .dlvReqDt(entity.getDlvReqDt() == null ? "" : entity.getDlvReqDt())
                .podrTotalAmt(entity.getPodrTotalAmt() == null ? 0  : entity.getPodrTotalAmt())
                .whsId(entity.getWhsId() == null ? "" : entity.getWhsId())
                .whsNm(entity.getWhsNm() == null ? "" : entity.getWhsNm() + "-" + entity.getLocationNm())
                .location(entity.getLocation() == null ? "" : entity.getLocation())
                .deliTelNo(entity.getDeliTelNo() == null ? "" : entity.getDeliTelNo())
                .invPoInfoList(dtlList.stream()
                        .map(m -> SpplyInvInitResponse.InvPoDtlInfo.builder()
                                .pchPodrUnqNo(m.getPchPodrUnqNo() == null ? "" : m.getPchPodrUnqNo())
                                .pchPodrDtlNo(m.getPchReqDtlNo() == null ? "" : m.getPchPodrDtlNo())
                                .itemId(m.getItemId() == null ? "" :m.getItemId())
                                .itemNm(m.getItemNm() == null ? "" :m.getItemNm())
                                .unit(m.getUnit() == null ? "" : m.getUnit())
                                .podrQty(m.getPodrQty() == null ? 0 :m.getPodrQty())
                                .shmtQty(m.getShmtQty() == null ? 0 : m.getShmtQty())
                                .expryYn(m.getExpryYn() == null ? "N" : m.getExpryYn())
                                .expryPeriod(m.getExpryPeriod() == null ? 0 : m.getExpryPeriod())
                                .expryDate(m.getExpryDate() == null ? "" : m.getExpryDate())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class InvPoDtlInfo {
        @ApiModelProperty(value = "구매발주번호") private String pchPodrUnqNo;
        @ApiModelProperty(value = "구매발주항번") private String pchPodrDtlNo;
        @ApiModelProperty(value = "품목아이디") private String itemId;
        @ApiModelProperty(value = "품목명") private String itemNm;
        @ApiModelProperty(value = "단위") private String unit;
        @ApiModelProperty(value = "발주수량") private Integer podrQty;
        @ApiModelProperty(value = "납품수량") private Integer shmtQty;
        @ApiModelProperty(value = "유통기한여부") private String expryYn;
        @ApiModelProperty(value = "유통기간") private Integer expryPeriod;
        @ApiModelProperty(value = "유통일자") private String expryDate;
    }

}
