package com.oqpo.api.web.dto.response.rtnmng.rtnReq;

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.PchReqBaseInfoEntity;
import com.oqpo.api.entity.pomng.PchReqDtlPtclEntity;
import com.oqpo.api.entity.rtnmng.RtnReqBaseInfoEntity;
import com.oqpo.api.entity.rtnmng.RtnReqDtlPtclEntity;
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 RtnReqInfoResponse {

    @ApiModelProperty(value = "반품요청고유번호") private String rtnReqUnqNo;
    @ApiModelProperty(value = "브랜드아이디") private String brandId;
    @ApiModelProperty(value = "브랜드명") private String brandNm;
    @ApiModelProperty(value = "매장아이디") private String storeId;
    @ApiModelProperty(value = "매장명") private String storeNm;
    @ApiModelProperty(value = "반품요청일자") private String rtnReqDt;
    @ApiModelProperty(value = "반품요청상태코드") private String rtnReqStCd;
    @ApiModelProperty(value = "반품요청상태명") private String rtnReqStNm;
    @ApiModelProperty(value = "반품요청담당자아이디") private String rtnReqMgrId;
    @ApiModelProperty(value = "반품요청담당자명") private String rtnReqMgrNm;
    @ApiModelProperty(value = "창고구분") private String whsDvsn;
    @ApiModelProperty(value = "수거요청일자") private String colReqDt;
    @ApiModelProperty(value = "비고") private String note;
    @ApiModelProperty(value = "반품반려일자") private String rtnRjctDt;
    @ApiModelProperty(value = "반품반려자명") private String rtnRjctNm;
    @ApiModelProperty(value = "반품반려자아이디") private String rtnRjctId;
    @ApiModelProperty(value = "반품반려사유") private String rtnRjctRsn;
    @ApiModelProperty(value = "반품요청금액") private Long rtnReqAmt;
    @ApiModelProperty(value = "반품요청품목수") private Integer rtnReqItemQty;
    @ApiModelProperty(value = "반품창고아이디") private String rtnWhsId;
    @ApiModelProperty(value = "반품창고명") private String rtnWhsNm;
    @ApiModelProperty(value = "반품LOCATION") private String rtnLocation;
    @ApiModelProperty(value = "반품LOCATION명") private String rtnLocationNm;


    @ApiModelProperty(value = "반품요청 품목 정보")
    private List<RtnReqInfoResponse.RtnReqDtlInfo> rtnReqDtlList;

    public static RtnReqInfoResponse toDTO(RtnReqBaseInfoEntity entity, List<RtnReqDtlPtclEntity>  dtlEntityList) {
        if (entity == null) return null;
        return RtnReqInfoResponse.builder()
                .rtnReqUnqNo(entity.getRtnReqUnqNo() == null ? "" :entity.getRtnReqUnqNo())
                .brandId(entity.getBrandId() == null ? "" :entity.getBrandId())
                .brandNm(entity.getBrandNm() == null ? "" :entity.getBrandNm())
                .storeId(entity.getStoreId() == null ? "" :entity.getStoreId())
                .storeNm(entity.getStoreNm() == null ? "" :entity.getStoreNm())
                .rtnReqDt(entity.getRtnReqDt() == null ? "" :entity.getRtnReqDt())
                .rtnReqStCd(entity.getRtnReqStCd() == null ? "" :entity.getRtnReqStCd())
                .rtnReqStNm(entity.getRtnReqStNm() == null ? "" :entity.getRtnReqStNm())
                .rtnReqMgrId(entity.getRtnReqMgrId() == null ? "" :entity.getRtnReqMgrId())
                .rtnReqMgrNm(entity.getRtnReqMgrNm() == null ? "" :entity.getRtnReqMgrNm())
                .whsDvsn(entity.getWhsDvsn() == null ? "" :entity.getWhsDvsn())
                .colReqDt(entity.getColReqDt() == null ? "" :entity.getColReqDt())
                .note(entity.getNote() == null ? "" :entity.getNote())
                .rtnRjctDt(entity.getRtnRjctDt() == null ? "" :entity.getRtnRjctDt())
                .rtnRjctNm(entity.getRtnRjctNm() == null ? "" :entity.getRtnRjctNm())
                .rtnRjctId(entity.getRtnRjctId() == null ? "" :entity.getRtnRjctId())
                .rtnRjctRsn(entity.getRtnRjctRsn() == null ? "" :entity.getRtnRjctRsn())
                .rtnReqAmt(entity.getRtnReqAmt() == null ? 0 :entity.getRtnReqAmt())
                .rtnReqItemQty(entity.getRtnReqItemQty() == null ? 0 :entity.getRtnReqItemQty())
                .rtnWhsId(entity.getRtnWhsId() == null ? "" :entity.getRtnWhsId())
                .rtnWhsNm(entity.getRtnWhsNm() == null ? "" :entity.getRtnWhsNm() + " - " + entity.getRtnLocationNm())
                .rtnLocation(entity.getRtnLocation() == null ? "" :entity.getRtnLocation())
                .rtnLocationNm(entity.getRtnLocationNm() == null ? "" : entity.getRtnLocationNm())
                .rtnReqDtlList(dtlEntityList.stream()
                        .map(m -> RtnReqDtlInfo.builder()
                                .rtnReqUnqNo(m.getRtnReqUnqNo() == null ? "" :m.getRtnReqUnqNo())
                                .rtnReqDtlNo(m.getRtnReqDtlNo() == null ? "" :m.getRtnReqDtlNo())
                                .itemId(m.getItemId() == null ? "" :m.getItemId())
                                .itemNm(m.getItemNm() == null ? "" :m.getItemNm())
                                .unit(m.getUnit() == null ? "" :m.getUnit())
                                .rtnQty(m.getRtnQty() == null ? 0 :m.getRtnQty())
                                .rtnAmt(m.getRtnAmt() == null ? 0 :m.getRtnAmt())
                                .rtnDvsn(m.getRtnDvsn() == null ? "" :m.getRtnDvsn())
                                .rtnDvsnNm(m.getRtnDvsnNm() == null ? "" :m.getRtnDvsnNm())
                                .rtnRsn(m.getRtnRsn() == null ? "" :m.getRtnRsn())
                                .spplyId(m.getSpplyId() == null ? "" :m.getSpplyId())
                                .unitAmt(m.getUnitAmt() == null ? 0 :m.getUnitAmt())
                                .storeUnitUnqNo(m.getStoreUnitUnqNo() == null ? "" :m.getStoreUnitUnqNo())
                                .rtnRsnDvsn(m.getRtnRsnDvsn() == null ? "" :m.getRtnRsnDvsn())
                                .rtnRsnDvsnNm(m.getRtnRsnDvsnNm() == null ? "" :m.getRtnRsnDvsnNm())
                                .brandUnitUnqNo(m.getBrandUnitUnqNo() == null ? "" :m.getBrandUnitUnqNo())
                                .unitGubun(m.getUnitGubun() == null ? "" :m.getUnitGubun())
                                .rtnReqDtlStCd(m.getRtnReqDtlStCd() == null ? "" : m.getRtnReqDtlStCd())
                                .rtnReqDtlStNm(m.getRtnReqDtlStNm() == null ? "" : m.getRtnReqDtlStNm())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class RtnReqDtlInfo {

        @ApiModelProperty(value = "반품요청고유번호") private String rtnReqUnqNo;
        @ApiModelProperty(value = "반품요청상세번호") private String rtnReqDtlNo;
        @ApiModelProperty(value = "품목아이디") private String itemId;
        @ApiModelProperty(value = "품목명") private String itemNm;
        @ApiModelProperty(value = "단위") private String unit;
        @ApiModelProperty(value = "반품수량") private Integer rtnQty;
        @ApiModelProperty(value = "반품금액") private Long rtnAmt;
        @ApiModelProperty(value = "반품구분") private String rtnDvsn;
        @ApiModelProperty(value = "반품구분명") private String rtnDvsnNm;
        @ApiModelProperty(value = "반품사유") private String rtnRsn;
        @ApiModelProperty(value = "공급사아이디") private String spplyId;
        @ApiModelProperty(value = "단가금액") private Long unitAmt;
        @ApiModelProperty(value = "매장단가고유번호") private String storeUnitUnqNo;
        @ApiModelProperty(value = "반품사유구분") private String rtnRsnDvsn;
        @ApiModelProperty(value = "반품사유구분명") private String rtnRsnDvsnNm;
        @ApiModelProperty(value = "브랜드단가고유번호") private String brandUnitUnqNo;
        @ApiModelProperty(value = "단가구분") private String unitGubun;
        @ApiModelProperty(value = "반품상세코드") private String rtnReqDtlStCd;
        @ApiModelProperty(value = "반품상세코드명") private String rtnReqDtlStNm;
    }
}
