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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.pomng.PchOdrBaseInfoEntity;
import com.oqpo.api.entity.pomng.PchOdrDtlPtclEntity;
import com.oqpo.api.entity.rtnmng.RtnOdrBaseInfoEntity;
import com.oqpo.api.entity.rtnmng.RtnOdrDtlPtclEntity;
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 SpplyPickInitResponse {

    @ApiModelProperty(value = "브랜드아이디") private String brandId;
    @ApiModelProperty(value = "브랜드명") private String brandNm;
    @ApiModelProperty(value = "수거요청일") private String colReqDt;
    @ApiModelProperty(value = "수거총금액") private Long rtnDodrTotalAmt;  // dlv_mgr_tel_no
    @ApiModelProperty(value = "수거창고아이디") private String rtnWhsId;
    @ApiModelProperty(value = "수거창고명") private String rtnWhsNm;
    @ApiModelProperty(value = "location") private String rtnLocation;
    @ApiModelProperty(value = "수거장소전화번호") private String pickTelNo;
    @ApiModelProperty(value = "반품발주번호") private String rtnPodrUnqNo;


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

    public static SpplyPickInitResponse toDTO(RtnOdrBaseInfoEntity entity, List<RtnOdrDtlPtclEntity>  dtlList) {
        if (entity == null) return null;
        return SpplyPickInitResponse.builder()
                .brandId(entity.getBrandId() == null ? "" : entity.getBrandId())
                .brandNm(entity.getBrandNm() == null ? "" : entity.getBrandNm())
                .rtnPodrUnqNo(entity.getRtnPodrUnqNo() == null ? "" : entity.getRtnPodrUnqNo())
                .colReqDt(entity.getColReqDt() == null ? "" : entity.getColReqDt())
                .rtnDodrTotalAmt(entity.getRtnDodrTotalAmt() == null ? 0  : entity.getRtnDodrTotalAmt())
                .rtnWhsId(entity.getRtnWhsId() == null ? "" : entity.getRtnWhsId())
                .rtnWhsNm(entity.getRtnWhsNm() == null ? "" : entity.getRtnWhsNm() + "-" + entity.getRtnLocationNm())
                .rtnLocation(entity.getRtnLocation() == null ? "" : entity.getRtnLocation())
                .pickTelNo(entity.getPickTelNo() == null ? "" : entity.getPickTelNo())
                .pickRtnDtlInfo(dtlList.stream()
                        .map(m -> PickRtnDtlInfo.builder()
                                .rtnPodrUnqNo(m.getRtnPodrUnqNo() == null ? "" : m.getRtnPodrUnqNo())
                                .rtnPodrDtlNo(m.getRtnPodrDtlNo() == null ? "" : m.getRtnPodrDtlNo())
                                .itemId(m.getItemId() == null ? "" :m.getItemId())
                                .itemNm(m.getItemNm() == null ? "" :m.getItemNm())
                                .unit(m.getUnit() == null ? "" : m.getUnit())
                                .unitAmt(m.getUnitAmt() == null ? 0 : m.getUnitAmt())
                                .rtnOdrQty(m.getRtnOdrQty() == null ? 0 :m.getRtnOdrQty())
                                .colQty(m.getColQty() == null ? 0 : m.getColQty())
                                .rtnRsnDvsn(m.getRtnRsnDvsn() == null ? "" : m.getRtnRsnDvsn())
                                .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 PickRtnDtlInfo {
        @ApiModelProperty(value = "반품발주번호") private String rtnPodrUnqNo;
        @ApiModelProperty(value = "반품발주항번") private String rtnPodrDtlNo;
        @ApiModelProperty(value = "품목아이디") private String itemId;
        @ApiModelProperty(value = "품목명") private String itemNm;
        @ApiModelProperty(value = "단위") private String unit;
        @ApiModelProperty(value = "단가") private Long unitAmt;
        @ApiModelProperty(value = "반품발주수량") private Integer rtnOdrQty;
        @ApiModelProperty(value = "수거수량") private Integer colQty;
        @ApiModelProperty(value = "반품사유") private String rtnRsnDvsn;
        @ApiModelProperty(value = "반품사유명") private String rtnRsnDvsnNm;
        @ApiModelProperty(value = "비고") private String rtnRsn;
    }

}
