package com.icomsys.main_vm.biz.common.system.vo;

import com.icomsys.main_vm.biz.common.login.res.UserVo;
import com.icomsys.main_vm.db.jpa.entity.system.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@NoArgsConstructor
public class SystemCustSaveReq {

    private String custName;
    private String custCode;
    private String custCodeDoubleCheck;
    private String custUseYn;
    private List<SystemCustSaveSubReq> addServiceAuthList;

    @Builder
    public SystemCustSaveReq(String custName, String custCode, String custCodeDoubleCheck, String custUseYn, List<SystemCustSaveSubReq> addServiceAuthList) {
        this.custName = custName;
        this.custCode = custCode;
        this.custCodeDoubleCheck = custCodeDoubleCheck;
        this.custUseYn = custUseYn;
        this.addServiceAuthList = addServiceAuthList;
    }



    public TbIcsCustInfo otCust(UserVo uv) {
        TbIcsCustInfo custSave = TbIcsCustInfo.builder()
                .custCode(this.custCode)
                .custName(this.custName)
//                .custTel()
//                .custServiceMent()
                .useYn(this.custUseYn)
                .registId(uv.getUserId())
                .registDate(LocalDateTime.now())
                .updateId(uv.getUserId())
                .updateDate(LocalDateTime.now())
                .build();
        return custSave;
    }

    public List<TbServiceGrouop> toServiceGroup(UserVo uv) {
        List<TbServiceGrouop> serviceGroupSave = new ArrayList<>();

        for (SystemCustSaveSubReq e : this.addServiceAuthList) {
            serviceGroupSave.add(TbServiceGrouop.builder()
                    .serviceGroup(e.getAddServiceGroup())
                    .serviceGroupUpper(e.getAddServiceGroup())
                    .custCode(this.custCode)
                    .serviceType("M")
                    .serviceGroupName(e.getAddServiceGroupName())
                    .serviceGroupDesc(e.getAddServiceGroupDesc())
                    .useYn("Y")
                    .registId(uv.getUserId())
                    .registDate(LocalDateTime.now())
                    .updateId(uv.getUserId())
                    .updateDate(LocalDateTime.now())
                    .build());
        }
        return serviceGroupSave;
    }

    public List<WisenutInsertReqVo> toWisenutInsert(){
        List<WisenutInsertReqVo> ret = new ArrayList<>();
        for (SystemCustSaveSubReq e : this.addServiceAuthList) {
            ret.add(WisenutInsertReqVo.builder()
                            .oprMngCode(this.custCode+"_RCP_"+e.getAddServiceGroup())
                            .serviceGroupDesc(e.getAddServiceGroupDesc())
                    .build());
        }
        return ret;
    }

    public List<TbIcsOprMngInfo> toOpr(UserVo uv) {
        List<TbIcsOprMngInfo> oprSave = new ArrayList<>();
        for (SystemCustSaveSubReq e : this.addServiceAuthList) {
            oprSave.add(TbIcsOprMngInfo.builder()
                    .custCode(this.custCode)
                    .serviceGroup(e.getAddServiceGroup())
                    .oprMngCode(e.getAddOprMngCode())
                    .solVal("RCP")
//                .dtmf()
//                .routeUrl()
                    .useYn("Y")
                    .registId(uv.getUserId())
                    .registDate(LocalDateTime.now())
                    .updateId(uv.getUserId())
                    .updateDate(LocalDateTime.now())
                    .build());
        }
        return oprSave;
    }

    public List<TbCustPolicy> toPolicy() {
        List<TbCustPolicy> custPolicySave = new ArrayList<>();

        for (SystemCustSaveSubReq e : this.addServiceAuthList) {
            for (String f : e.getAuthList()) {
                custPolicySave.add(TbCustPolicy.builder()
                        .serviceGroup(e.getAddServiceGroup())
                        .policyVal(f)
                        .custCode(this.custCode)
                        .build());
            }
        }
        return custPolicySave;
    }

    public List<TbUserCust> toSystemAdmin(UserVo uv) {
        List<TbUserCust> result = new ArrayList<>();
        for (SystemCustSaveSubReq e : this.addServiceAuthList) {
            result.add(TbUserCust.builder()
                    .custCode(this.custCode)
                    .serviceGroup(e.getAddServiceGroup())
                    .userSeq(uv.getUserSeq())
                    .build());
        }
        return result;
    }


    public TbIcsCustInfo ModiCust(UserVo uv) {
        TbIcsCustInfo custSave = TbIcsCustInfo.builder()
                .custCode(this.custCode)
                .custName(this.custName)
//                .custTel()
//                .custServiceMent()
                .useYn(this.custUseYn)
//                .registId(uv.getUserId())
//                .registDate(LocalDateTime.now())
                .updateId(uv.getUserId())
                .updateDate(LocalDateTime.now())
                .build();
        return custSave;
    }

    public List<TbServiceGrouop> ModiServiceGroup(UserVo uv) {
        List<TbServiceGrouop> serviceGroupSave = new ArrayList<>();

        for (SystemCustSaveSubReq e : this.addServiceAuthList) {
            serviceGroupSave.add(TbServiceGrouop.builder()
                    .serviceGroup(e.getAddServiceGroup())
                    .serviceGroupUpper(e.getAddServiceGroup())
                    .custCode(this.custCode)
                    .serviceType("M")
                    .serviceGroupName(e.getAddServiceGroupName())
                    .serviceGroupDesc(e.getAddServiceGroupDesc())
                    .useYn("Y")
//                    .registId(uv.getUserId())
//                    .registDate(LocalDateTime.now())
                    .updateId(uv.getUserId())
                    .updateDate(LocalDateTime.now())
                    .build());
        }
        return serviceGroupSave;
    }

    public List<TbIcsOprMngInfo> ModiOpr(UserVo uv) {
        List<TbIcsOprMngInfo> oprSave = new ArrayList<>();
        for (SystemCustSaveSubReq e : this.addServiceAuthList) {
            oprSave.add(TbIcsOprMngInfo.builder()
                    .custCode(this.custCode)
                    .serviceGroup(e.getAddServiceGroup())
                    .oprMngCode(e.getAddOprMngCode())
                    .solVal("RCP")
//                .dtmf()
//                .routeUrl()
                    .useYn("Y")
//                    .registId(uv.getUserId())
//                    .registDate(LocalDateTime.now())
                    .updateId(uv.getUserId())
                    .updateDate(LocalDateTime.now())
                    .build());
        }
        return oprSave;
    }

}
