package com.oqpo.api.web.dto.response.pomng.pchOrd;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity;
import com.oqpo.api.entity.oper.UserMngEntity;
import com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity;
import com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity;
import com.oqpo.api.entity.pomng.PchReqBaseInfoEntity;
import com.oqpo.api.entity.pomng.PchReqDtlPtclEntity;
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 PchOdrInfoResponse {

    @ApiModelProperty(value = "브랜드아이디") private String brandId;
    @ApiModelProperty(value = "브랜드명") private String brandNm;
    @ApiModelProperty(value = "구매발주고유번호") private String pchPodrUnqNo;
    @ApiModelProperty(value = "구매발주상태코드") private String pchOdrStCd;
    @ApiModelProperty(value = "구매발주상태명") private String pchOdrStNm;
    @ApiModelProperty(value = "공급사아이디") private String spplyId;
    @ApiModelProperty(value = "공급사명") private String spplyNm;
    @ApiModelProperty(value = "발주일자") private String podrDt;
    @ApiModelProperty(value = "납품창고") private String whsDvsnNm;
    @ApiModelProperty(value = "출고창고") private String shmtWhsDvsnNm;
    @ApiModelProperty(value = "발주합계금액") private Long podrTotalAmt;
    @ApiModelProperty(value = "발주품목수량") private Integer podrItemQty;
    @ApiModelProperty(value = "발주담당자명") private String podrMgrNm;
    @ApiModelProperty(value = "발주담당자전화번호") private String podrMgrTelNo;
    @ApiModelProperty(value = "배송구분명") private String dlvDvsnNm;
    @ApiModelProperty(value = "납품요청일") private String dlvReqDt;
    @ApiModelProperty(value = "매장아이디") private String storeId;

    @ApiModelProperty(value = "구매발주 품목 정보")
    private List<PchOdrInfoResponse.PchOrdPoDtlInfo> pchOdrDtlList;

    public static PchOdrInfoResponse toDTO(PchOdrBaseInfoEntity entity, List<PchOdrDtlPtclEntity>  dtlEntityList) {
        if (entity == null) return null;
        return PchOdrInfoResponse.builder()
                .brandId(entity.getBrandId() == null ? "" : entity.getBrandId())
                .brandNm(entity.getBrandNm() == null ? "" : entity.getBrandNm())
                .pchPodrUnqNo(entity.getPchPodrUnqNo() == null ? "" : entity.getPchPodrUnqNo())
                .pchOdrStCd(entity.getPchOdrStCd() == null ? "" : entity.getPchOdrStCd())
                .pchOdrStNm(entity.getPchOdrStNm() == null ? "" : entity.getPchOdrStNm())
                .spplyId(entity.getSpplyId() == null ? "" : entity.getSpplyId())
                .spplyNm(entity.getSpplyNm() == null ? "" : entity.getSpplyNm())
                .podrDt(entity.getPodrDt() == null ? "" : entity.getPodrDt())
                .whsDvsnNm(entity.getWhsDvsnNm() == null ? "" : entity.getWhsDvsnNm() + "-" + entity.getLocationNm())
                .shmtWhsDvsnNm(entity.getShmtWhsNm() == null ? "" : entity.getShmtWhsNm() + "-" + entity.getShmtLocationNm())
                .podrTotalAmt(entity.getPodrTotalAmt() == null ? 0 : entity.getPodrTotalAmt())
                .podrItemQty(entity.getPodrItemQty() == null ? 0 : entity.getPodrItemQty())
                .podrMgrNm(entity.getPodrMgrNm() == null ? "" : entity.getPodrMgrNm())
                .podrMgrTelNo(entity.getPodrMgrTelNo() == null ? "" : entity.getPodrMgrTelNo())
                .dlvDvsnNm(entity.getDlvDvsnNm() == null ? "" : entity.getDlvDvsnNm())
                .dlvReqDt(entity.getDlvReqDt() == null ? "" : entity.getDlvReqDt())
                .storeId(entity.getStoreId() == null ? "" : entity.getStoreId())
                .pchOdrDtlList(dtlEntityList.stream()
                        .map(m -> PchOrdPoDtlInfo.builder()
                                .pchPodrUnqNo(m.getPchPodrUnqNo())
                                .pchPodrDtlNo(m.getPchPodrDtlNo())
                                .itemId(m.getItemId() == null ? "" : m.getItemId())
                                .itemNm(m.getItemNm() == null ? "" : m.getItemNm())
                                .podrQty(m.getPodrQty() == null ? 0 : m.getPodrQty())
                                .podrAmt(m.getPodrAmt() == null ? 0 : m.getPodrAmt())
                                .unit(m.getUnit() == null ? "" : m.getUnit())
                                .unitAmt(m.getUnitAmt() == null ? 0 : m.getUnitAmt())
                                .whsPassQty(m.getWhsPassQty() == null ? 0  : m.getWhsPassQty())
                                .podrDtlStCd(m.getPodrDtlStCd() == null ? "" : m.getPodrDtlStCd())
                                .podrDtlStNm(m.getPodrDtlStNm() == null ? "" : m.getPodrDtlStNm())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class PchOrdPoDtlInfo {
        @ApiModelProperty(value = "구매발주고유번호") private String pchPodrUnqNo;
        @ApiModelProperty(value = "구매발주상세번호") private String pchPodrDtlNo;
        @ApiModelProperty(value = "품목아이디") private String itemId;
        @ApiModelProperty(value = "품목명") private String itemNm;
        @ApiModelProperty(value = "발주수량") private Integer podrQty;
        @ApiModelProperty(value = "발주금액") private Long podrAmt;
        @ApiModelProperty(value = "단위") private String unit;
        @ApiModelProperty(value = "단가금액") private Long unitAmt;
        @ApiModelProperty(value = "출고가능수량") private Integer whsPassQty;
        @ApiModelProperty(value = "발주상세코드") private String podrDtlStCd;
        @ApiModelProperty(value = "발주상세명") private String podrDtlStNm;
    }
}
