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


import com.google.gson.Gson;
import com.icomsys.main_vm.biz.advice.excep.CustomBadRequestException;
import com.icomsys.main_vm.biz.advice.excep.CustomNotFoundException;
import com.icomsys.main_vm.biz.common.login.service.LoginService;
import com.icomsys.main_vm.biz.common.system.vo.*;
import com.icomsys.main_vm.db.jpa.entity.system.TbBotCommonCode;
import com.icomsys.main_vm.db.jpa.entity.system.TbServiceGrouop;
import com.icomsys.main_vm.db.jpa.entity.system.id.TbBotCommonCodeId;
import com.icomsys.main_vm.db.jpa.repo.system.TbBotCommonCodeRepo;
import com.icomsys.main_vm.db.jpa.repo.system.TbServiceGrouopRepo;
import com.querydsl.core.QueryResults;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.analysis.function.Add;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class SystemCommoncodeService {

    private final LoginService loginService;
    private final TbBotCommonCodeRepo tbBotCommonCodeRepo;
    private final TbServiceGrouopRepo tbServiceGrouopRepo;

    @Transactional(readOnly = true)
    public QueryResults<SystemCommoncodeRes> SystemCommoncodeList(SystemCommoncodeReq dto) {
        QueryResults<SystemCommoncodeRes> result = tbBotCommonCodeRepo
                .SystemCommoncodeList(dto.ofUserVo(loginService.getUserVo()));
        return result;
    }

    @Transactional
    public void SystemCommoncodeInsert(SystemCommoncodeInsertReq dto) throws CustomBadRequestException, CustomNotFoundException {

        log.info("insert - {}", new Gson().toJson(dto));

        TbServiceGrouop service = tbServiceGrouopRepo.findByServiceGroup(dto.getServiceGroup())
                .orElseThrow(() -> new CustomNotFoundException());
        if (tbBotCommonCodeRepo.existsByOprMngCodeAndMajorCodeAndMinorCode(dto.getOpr(loginService.getUserVo()), dto.getMajor(), dto.getMinor())) {
            throw new CustomBadRequestException();
        } else {
//            if (dto.getCodeType().equals("C_CD")) {
                tbBotCommonCodeRepo.save(dto.ofSave(loginService.getUserVo(), service.getCustCode(), service.getServiceGroup()));
//                tbBotCommonCodeRepo.saveAll(dto.ofSaveAll(loginService.getUserVo(), tbServiceGrouopRepo.findByServiceGroupUpperAndServiceType(tbServiceGrouopRepo
//                                .findByServiceGroup(dto.getServiceGroup()).orElseThrow(() -> new CustomNotFoundException()).getServiceGroupUpper(), "S")
//                        .stream().map(f -> f.getServiceGroup()).collect(Collectors.toList())));
//            } else {
//                tbBotCommonCodeRepo.save(dto.ofSave(loginService.getUserVo(), service.getCustCode()));
        }
    }

    @Transactional
    public void SystemCommoncodeUpdate(SystemCommoncodeUpdateReq dto) throws CustomNotFoundException {

        log.info("update - {}", new Gson().toJson(dto));

//        if (dto.getCodeType().equals("C_CD")) {
//            for (TbBotCommonCodeId f : tbServiceGrouopRepo.findByServiceGroupUpperAndServiceType(tbServiceGrouopRepo.findByServiceGroup(dto.getServiceGroup())
//                            .orElseThrow(() -> new CustomNotFoundException()).getServiceGroupUpper(), "S")
//                    .stream()
//                    .map(e -> TbBotCommonCodeId.builder().oprMngCode(e.getCustCode() + "_RCP_" + e.getServiceGroup())
//                            .majorCode(dto.getMajorCode()).minorCode(dto.getMinorCode()).build()).collect(Collectors.toList())) {
//                tbBotCommonCodeRepo.findById(f)
//                        .orElseThrow(() -> new CustomNotFoundException())
//                        .toUpdate(dto, loginService.getUserVo());
//            }
//        } else {
            tbBotCommonCodeRepo.findById(dto.toId())
                    .orElseThrow(() -> new CustomNotFoundException())
                    .toUpdate(dto, loginService.getUserVo());
//        }
    }

    public void SystemCommoncodeDelete(@RequestBody SystemCommonCodeDeleteReq dto) {

        tbBotCommonCodeRepo.deleteById(dto.delId());
    }

}
