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

import com.fasterxml.jackson.annotation.JsonInclude;
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.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 RtnOrdInitInfoResponse {

    @ApiModelProperty(value = "브랜드아이디") private String brandId;
    @ApiModelProperty(value = "브랜드명") private String brandNm;
    @ApiModelProperty(value = "요청반품구분") private String rtnReqDvsn;  // RR01 요청반품
    @ApiModelProperty(value = "반품총금액") private Long rtnDodrTotalAmt;
    @ApiModelProperty(value = "반품총품목수") private Integer rtnDodrItemQty;
    @ApiModelProperty(value = "반품담당자명") private String rtnMgrNm;
    @ApiModelProperty(value = "반품담당자아이디") private String rtnMgrId;
    @ApiModelProperty(value = "반품담당자전화번호") private String rtnMgrTelNo;
    @ApiModelProperty(value = "공급사아이디") private String spplyId;
    @ApiModelProperty(value = "공급사명") private String spplyNm;
    @ApiModelProperty(value = "매장아이디") private String storeId;
    @ApiModelProperty(value = "수거요청일") private String colReqDt;
    @ApiModelProperty(value = "입고창고 아이디") private String rtnWhsId;
    @ApiModelProperty(value = "입고창고 location") private String rtnLocation;
    @ApiModelProperty(value = "입고창고 명") private String rtnWhsNm;
    @ApiModelProperty(value = "입고창고 location명") private String rtnLocationNm;
    @ApiModelProperty(value = "수거구분") private String colDvsn;
    @ApiModelProperty(value = "수거구분명") private String colDvsnNm;

    //RTN_PLACE  RTN_MGR_EMAL  RTN_MGR_MBL_NO 삭제
    @ApiModelProperty(value = "반품품목리스트") private List<RtnOrdDtlInfo> itemList;


    public static RtnOrdInitInfoResponse toDTO(UserMngEntity uEntity, RtnOdrBaseInfoEntity baseEntity, List<RtnOdrDtlPtclEntity> dtlEntity ) {
        if (uEntity == null) return null;
        return RtnOrdInitInfoResponse.builder()
                .brandId(baseEntity.getBrandId() == null ? "" : baseEntity.getBrandId())
                .brandNm(baseEntity.getBrandNm() == null ? "" : baseEntity.getBrandNm())
                .rtnReqDvsn("RR01")
                .rtnDodrTotalAmt(baseEntity.getRtnDodrTotalAmt() == null ? 0 : baseEntity.getRtnDodrTotalAmt())
                .rtnDodrItemQty(baseEntity.getRtnDodrItemQty() == null ? 0 : baseEntity.getRtnDodrItemQty())
                .rtnMgrNm(uEntity.getUserNm() == null ? "" : uEntity.getUserNm())
                .rtnMgrTelNo(uEntity.getTelNo() == null ? "" : uEntity.getTelNo())
                .rtnMgrId(uEntity.getUserId() == null ? "" : uEntity.getUserId())
                .spplyId(baseEntity.getSpplyId() == null ? "" : baseEntity.getSpplyId())
                .spplyNm(baseEntity.getSpplyNm() == null ? "" : baseEntity.getSpplyNm())
                .storeId(baseEntity.getStoreId() == null ? "" : baseEntity.getStoreId())
                .colReqDt(baseEntity.getColReqDt() == null ? "" : baseEntity.getColReqDt())
                .rtnWhsId(baseEntity.getRtnWhsId() == null ? "" : baseEntity.getRtnWhsId())
                .rtnWhsNm(baseEntity.getRtnWhsNm() == null ? "" : baseEntity.getRtnWhsNm())
                .rtnLocation(baseEntity.getRtnLocation() == null ? "" : baseEntity.getRtnLocation())
                .rtnLocationNm(baseEntity.getRtnLocationNm() == null ? "" : baseEntity.getRtnLocationNm())
                .colDvsn(baseEntity.getColDvsn() == null ? "" : baseEntity.getColDvsn())
                .colDvsnNm(baseEntity.getColDvsnNm() == null ? "" : baseEntity.getColDvsnNm())
                .itemList(dtlEntity.stream()
                        .map(m -> RtnOrdDtlInfo.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())
                                .unitAmt(m.getUnitAmt() == null ? 0 : m.getUnitAmt())
                                .unit(m.getUnit() == null ? "" : m.getUnit())
                                .rtnReqQty(m.getRtnQty() == null ? 0 : m.getRtnQty())
                                .rtnQty(m.getRtnQty() == null ? 0 : m.getRtnQty())
                                .rtnAmt(m.getRtnAmt() == null ? 0 : m.getRtnAmt())
                                .brandUnitUnqNo(m.getBrandUnitUnqNo() == null ? "" : m.getBrandUnitUnqNo())
                                .storeUnitUnqNo(m.getStoreUnitUnqNo() == null ? "" : m.getStoreUnitUnqNo())
                                .rtnRsnDvsn(m.getRtnRsnDvsn())
                                .rtnRsnDvsn(m.getRtnRsnDvsn() == null ? "" : m.getRtnRsnDvsn())
                                .rtnRsnDvsnNm(m.getRtnRsnDvsnNm() == null ? "" : m.getRtnRsnDvsnNm())
                                .rtnRsn(m.getRtnRsn() == null ? "" : m.getRtnRsn())
                                .unitGubun(m.getUnitGubun() == null ? "" : m.getUnitGubun())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }


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

        @ApiModelProperty(value = "반품요청번호")
        private String rtnReqUnqNo;

        @ApiModelProperty(value = "반품요청항번")
        private String rtnReqDtlNo;

        @ApiModelProperty(value = "품목아이디")
        private String itemId;

        @ApiModelProperty(value = "품목명")
        private String itemNm;

        @ApiModelProperty(value = "단가")
        private Long unitAmt;

        @ApiModelProperty(value = "단위")
        private String unit;

        @ApiModelProperty(value = "반품요청수량")
        private Integer rtnReqQty;

        @ApiModelProperty(value = "반품발주수량")
        private Integer rtnQty;

        @ApiModelProperty(value = "반품발주금액")
        private Long rtnAmt;

        @ApiModelProperty(value = "브랜드단가고유번호")
        private String brandUnitUnqNo;

        @ApiModelProperty(value = "매장단가고유번호")
        private String storeUnitUnqNo;

        @ApiModelProperty(value = "반품사유구분") private String rtnRsnDvsn;
        @ApiModelProperty(value = "반품사유구분명") private String rtnRsnDvsnNm;
        @ApiModelProperty(value = "반품사유내용") private String rtnRsn;
        @ApiModelProperty(value = "단가구분") private String unitGubun;

    }
}
