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

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.stinfo.BrandEntity;
import com.oqpo.api.entity.stockmng.WhsMgntBaseInfoEntity;
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 PchReqInitInfoResponse {

    @ApiModelProperty(value = "구매요청번호") private String pchReqUnqNo;
    @ApiModelProperty(value = "로그인 소속 브랜드아이디") private String brandId;
    @ApiModelProperty(value = "로그인 소속 브랜드명") private String brandNm;
    @ApiModelProperty(value = "로그인 소속 매장아이디") private String storeId;
    @ApiModelProperty(value = "로그인 소속 매장명") private String storeNm;
    @ApiModelProperty(value = "소속") private String reqDept;
    @ApiModelProperty(value = "사용자명") private String userNm;
    @ApiModelProperty(value = "권한타입코드") private String authTpCd;
    @ApiModelProperty(value = "정산타입") private String loanDvsn;
    @ApiModelProperty(value = "정산타입명") private String loanDvsnNm;
    @ApiModelProperty(value = "구매가능금액") private long ordUseAmt;
    @ApiModelProperty(value = "매장고유여신번호") private String loanMgntUnqNo;
    @ApiModelProperty(value = "구매요청구분 (매장구매 PD01 브랜드 PD03 ") private String pchReqDvsn;

    @ApiModelProperty(value = "PG사 코드") private String pgId;
    @ApiModelProperty(value = "PG사 명") private String pgNm;
    @ApiModelProperty(value = "PG 브랜드그룹 아이디") private String mallId;


    @ApiModelProperty(value = "창고아이디") private String whsId;
    @ApiModelProperty(value = "창고명") private String whsNm;
    @ApiModelProperty(value = "location") private String location;
    @ApiModelProperty(value = "location명") private String locationNm;

    @ApiModelProperty(value = "위시리스트사용여부") private String wishListUseYn;


    public static PchReqInitInfoResponse toDTO(UserMngEntity uEntity, LoanMgntBaseInfoEntity loanEntity, WhsMgntBaseInfoEntity whsEntity, BrandEntity pgInfo, String pchReqUnqNo) {
        if (uEntity == null) return null;
        return PchReqInitInfoResponse.builder()
                .pchReqUnqNo(pchReqUnqNo)
                .brandId(uEntity.getBrandId() == null ? "" :uEntity.getBrandId())
                .brandNm(uEntity.getBrandNm() == null ? "" :uEntity.getBrandNm())
                .storeId(uEntity.getStoreId() == null ? "" :uEntity.getStoreId())
                .storeNm(uEntity.getStoreNm() == null ? "" :uEntity.getStoreNm())
                .reqDept(uEntity.getStoreId() == null ? "브랜드" : "매장")
                .userNm(uEntity.getUserNm() == null ? "" : uEntity.getUserNm())
                .authTpCd(uEntity.getAuthTpCd() == null ? "" : uEntity.getAuthTpCd())
                .ordUseAmt(loanEntity == null ? 0 : loanEntity.getOrdUseAmt() == null ? 0 : loanEntity.getOrdUseAmt())
                .loanDvsn(loanEntity == null ?  "" : loanEntity.getLoanDvsn() == null ? "LD03" : loanEntity.getLoanDvsn())
                .loanDvsnNm(loanEntity == null ?  "" : loanEntity.getLoanDvsnNm() == null ? "무정산" : loanEntity.getLoanDvsnNm())
                .loanMgntUnqNo(loanEntity == null ?  "" : loanEntity.getLoanMgntUnqNo() == null ? "" : loanEntity.getLoanMgntUnqNo())
                .pgId(pgInfo == null ? "" : pgInfo.getPgId() == null ? "" : pgInfo.getPgId())
                .pgNm(pgInfo == null ? "" : pgInfo.getPgNm() == null ? "" : pgInfo.getPgNm())
                .mallId(pgInfo == null ? "" : pgInfo.getMallId() == null ? "" : pgInfo.getMallId())
                .pchReqDvsn(uEntity.getStoreId() == null || "".equals(uEntity.getStoreId()) ? "PD03" : "PD01")
                .whsId(whsEntity == null ? "" : whsEntity.getWhsId() == null ? "" : whsEntity.getWhsId())
                .whsNm(whsEntity == null ? "" : whsEntity.getWhsNm() == null ? "" : whsEntity.getWhsNm())
                .location(whsEntity == null ? "" : whsEntity.getLocationInfo() == null ? "" : whsEntity.getLocationInfo().getLocation() == null ? "" : whsEntity.getLocationInfo().getLocation())
                .locationNm(whsEntity == null ? "" : whsEntity.getLocationInfo() == null ? "" : whsEntity.getLocationInfo().getLocationNm() == null ? "" : whsEntity.getLocationInfo().getLocationNm())
                .wishListUseYn(uEntity.getAuthTpCd() == null ? "N" : "50".equals(uEntity.getAuthTpCd()) ? pgInfo == null ? "N" : pgInfo.getWishListUseYn() == null ? "N" : pgInfo.getWishListUseYn() : "N")
                .build();
    }

}
