package com.oqpo.api.web.dto.response.oper.affl;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.oqpo.api.entity.CodeEntity;
import com.oqpo.api.entity.oper.AfflEntity;
import com.oqpo.api.entity.oper.AfflSaleInfoEntity;
import com.oqpo.api.entity.oper.FileEntity;
import com.oqpo.api.web.dto.response.oper.ntice.NticeResponse;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
@Builder
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@JsonInclude(JsonInclude.Include.ALWAYS)
public class AfflShopInfoResponse {

    @ApiModelProperty(value = "가맹점아이디")
    private String afflShopId ;

    @ApiModelProperty(value = "가맹점명")
    private String afflShopNm ;

    @ApiModelProperty(value = "상태코드")
    private String stCd ;

    @ApiModelProperty(value = "상태명")
    private String stNm ;

    @ApiModelProperty(value = "약어")
    private String afflAbbr ;

    @ApiModelProperty(value = "사업자번호")
    private String bsnsRegNo ;

    @ApiModelProperty(value = "법인번호")
    private String corpRegNo ;

    @ApiModelProperty(value = "업태")
    private String bsnsCndt ;

    @ApiModelProperty(value = "업종")
    private String ctgrBsns ;

    @ApiModelProperty(value = "대표자명")
    private String ownerNm ;

    @ApiModelProperty(value = "대표자연락처")
    private String ownerTelNo ;

    @ApiModelProperty(value = "대표팩스번호")
    private String rprstFaxNo ;

    @ApiModelProperty(value = "대표이메일")
    private String rprstEmai ;

    @ApiModelProperty(value = "우편번호")
    private String zipNo ;

    @ApiModelProperty(value = "주소1")
    private String addr1 ;

    @ApiModelProperty(value = "주소2")
    private String addr2 ;

    @ApiModelProperty(value = "대표전화번호")
    private String rprstTelNo ;

    @ApiModelProperty(value = "x좌표")
    private String latX ;

    @ApiModelProperty(value = "Y좌표")
    private String longY ;

    @ApiModelProperty(value = "등록일")
    private String regDt ;

    @ApiModelProperty(value = "사용 시작일")
    private String joinSttDt ;

    @ApiModelProperty(value = "사용 종료일")
    private String joinEndDt ;

    @ApiModelProperty(value = "로고 이미지파일번호")
    private String cmpnyLogoFileNo ;

    @ApiModelProperty(value = "기업형태")
    private String corpType ;

    @ApiModelProperty(value = "기업형태명")
    private String corpTypeNm ;

    @ApiModelProperty(value = "로그파일 정보")
    private List<AfflShopInfoResponse.FileLogInfo> fileLogNoList;

    @ApiModelProperty(value = "영업담당자 리스트")
    private List<AfflSaleInfo> saleList;

    public static AfflShopInfoResponse toDTO(AfflEntity entity, List<AfflSaleInfoEntity> afflSaleList, List<FileEntity>  fileLogInfoList) {
        if (entity == null) return null;
        return AfflShopInfoResponse.builder()
                .afflShopId(entity.getAfflShopId())
                .afflShopNm(entity.getAfflShopNm())
                .stCd(entity.getStCd())
                .stNm(entity.getStNm())
                .afflAbbr(entity.getAfflAbbr() == null ? "" : entity.getAfflAbbr())
                .bsnsRegNo(entity.getBsnsRegNo() == null ? "" : entity.getBsnsRegNo())
                .corpRegNo(entity.getCorpRegNo() == null ? "" : entity.getCorpRegNo())
                .bsnsCndt(entity.getBsnsCndt() == null ? "" : entity.getBsnsCndt())
                .ctgrBsns(entity.getCtgrBsns() == null ? "" : entity.getCtgrBsns())
                .ownerNm(entity.getOwnerNm() == null ? "" : entity.getOwnerNm())
                .ownerTelNo(entity.getOwnerTelNo() == null ? "" : entity.getOwnerTelNo())
                .rprstFaxNo(entity.getRprstFaxNo() == null ? "" : entity.getRprstFaxNo())
                .rprstEmai(entity.getRprstEmai() == null ? "" : entity.getRprstEmai())
                .zipNo(entity.getZipNo() == null ? "" : entity.getZipNo())
                .addr1(entity.getAddr1() == null ? "" : entity.getAddr1())
                .addr2(entity.getAddr2() == null ? "" : entity.getAddr2())
                .rprstTelNo(entity.getRprstTelNo() == null ? "" : entity.getRprstTelNo())
                .latX(entity.getLatX() == null ? "" : entity.getLatX())
                .longY(entity.getLongY() == null ? "" : entity.getLongY())
                .regDt(entity.getRegDt() == null ? "" : entity.getRegDt())
                .joinSttDt(entity.getJoinSttDt() == null ? "" : entity.getJoinSttDt())
                .joinEndDt(entity.getJoinEndDt() == null ? "" : entity.getJoinEndDt())
                .cmpnyLogoFileNo(entity.getCmpnyLogoFileNo() == null ? "" : entity.getCmpnyLogoFileNo())
                .fileLogNoList(fileLogInfoList.stream()
                        .map(m -> AfflShopInfoResponse.FileLogInfo.builder()
                                .fileNo(m.getFileNo())
                                .fileSeq(m.getFileSeq())
                                .filePath(m.getFilePath())
                                .fileNm(m.getFileNm())
                                .fileOrgNm(m.getFileOrgNm())
                                .build())
                        .collect(Collectors.toList()))
                .corpType(entity.getCorpType() == null ? "" : entity.getCorpType())
                .corpTypeNm(entity.getCorpTypeNm() == null ? "" : entity.getCorpTypeNm())
                .saleList(afflSaleList.stream()
                        .map(m -> AfflSaleInfo.builder()
                                .afflShopId(m.getAfflShopId())
                                .saleMgrId(m.getSaleMgrId())
                                .saleMgrNm(m.getSaleMgrInfo().getSaleMgrNm())
                                .rprstTelNo(m.getSaleMgrInfo().getRprstTelNo() == null ? "" : m.getSaleMgrInfo().getRprstTelNo())
                                .rprstEmai(m.getSaleMgrInfo().getRprstEmai() == null ? "" : m.getSaleMgrInfo().getRprstEmai())
                                .stCd(m.getSaleMgrInfo().getStCd())
                                .stNm(m.getSaleMgrInfo().getStNm())
                                .build())
                        .collect(Collectors.toList()))
                .build();
    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class AfflSaleInfo {
        @ApiModelProperty(value = "가맹점아이디")
        private String afflShopId;

        @ApiModelProperty(value = "영업담당자아이디")
        private String saleMgrId;

        @ApiModelProperty(value = "영업담당자명")
        private String saleMgrNm;

        @ApiModelProperty(value = "대표전화번호")
        private String rprstTelNo;

       @ApiModelProperty(value = "대표이메일")
        private String rprstEmai;

        @ApiModelProperty(value = "상태코드")
        private String stCd;

        @ApiModelProperty(value = "상태명")
        private String stNm;

    }

    @Getter
    @Builder
    @AllArgsConstructor(access = AccessLevel.PROTECTED)
    static class FileLogInfo {
        @ApiModelProperty(value = "파일번호")
        private String fileNo;

        @ApiModelProperty(value = "파일순번")
        private Integer fileSeq;

        @ApiModelProperty(value = "파일경로")
        private String filePath;

        @ApiModelProperty(value = "파일명")
        private String fileNm;

        @ApiModelProperty(value = "원파일명")
        private String fileOrgNm;
    }
}
