package com.its.api.its.service.vms;

import com.its.api.its.repository.vms.TbVmsIfscCmtrGradRepository;
import com.its.api.its.model.dto.vms.TbVmsIfscCmtrGradDto;
import com.its.api.its.model.entity.vms.TbVmsIfscCmtrGrad;
import com.its.api.its.model.entity.vms.TbVmsIfscCmtrGradKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbVmsIfscCmtrGradService {

    private final TbVmsIfscCmtrGradRepository repo;

    // 데이터 1건 조회, 없으면 exception
    private TbVmsIfscCmtrGrad requireOne(TbVmsIfscCmtrGradKey id) {
        return repo.findById(id)
                .orElseThrow(() -> new NoSuchElementException("데이터가 존재하지 않습니다: " + id));
    }

    // 전체 데이터 조회
    public List<TbVmsIfscCmtrGradDto> findAll() {
        List<TbVmsIfscCmtrGradDto> result = new ArrayList<>();
        List<TbVmsIfscCmtrGrad> data = this.repo.findAll();
        for (TbVmsIfscCmtrGrad entity : data) {
            result.add(entity.toDto());
        }
        return result;
    }

    // 데이터 1건 조회(기존 데이터가 반드시 존재해야 함)
    public TbVmsIfscCmtrGradDto findById(TbVmsIfscCmtrGradKey id) {
        TbVmsIfscCmtrGrad entity = requireOne(id);
        return entity.toDto();
    }

    public List<TbVmsIfscCmtrGradDto> findDataById(Long id) {
        List<TbVmsIfscCmtrGradDto> result = new ArrayList<>();
        List<TbVmsIfscCmtrGrad> data = this.repo.findDataById(id);
        if (data != null) {
            for (TbVmsIfscCmtrGrad grad : data) {
                result.add(grad.toDto());
            }
        }
        return result;
    }

    // 데이터 변경
    @Transactional
    public TbVmsIfscCmtrGradDto updateById(TbVmsIfscCmtrGradKey id, TbVmsIfscCmtrGradDto.TbVmsIfscCmtrGradUpdReq req) {
        TbVmsIfscCmtrGrad entity = requireOne(id);
        entity.updateInfo(req);
        this.repo.save(entity);
        return entity.toDto();
    }

    // 데이터 변경 또는 생성-목록(데이터가 존재하면 업데이트 없으면 신규로 생성)
    @Transactional
    public List<TbVmsIfscCmtrGradDto> mergeInfoList(List<TbVmsIfscCmtrGradDto.TbVmsIfscCmtrGradUpdReq> reqList) {
        List<TbVmsIfscCmtrGradDto> result = new ArrayList<>();
        for (TbVmsIfscCmtrGradDto.TbVmsIfscCmtrGradUpdReq req : reqList) {
            TbVmsIfscCmtrGrad obj = req.toEntity();
            this.repo.save(obj);
            result.add(obj.toDto());
        }
        return result;
    }

    // 데이터 변경 또는 생성-개별(데이터가 존재하면 업데이트 없으면 신규로 생성)
    @Transactional
    public TbVmsIfscCmtrGradDto mergeInfo(TbVmsIfscCmtrGradDto.TbVmsIfscCmtrGradUpdReq req) {
        TbVmsIfscCmtrGrad obj = req.toEntity();
        this.repo.save(obj);
        return obj.toDto();
    }

    // 정보 삭제-개별, 데이터 존재하지 않으면 Exception
    @Transactional
    public TbVmsIfscCmtrGradDto deleteById(TbVmsIfscCmtrGradKey id) {
        TbVmsIfscCmtrGrad entity = requireOne(id);
        this.repo.deleteById(id);
        return entity.toDto();
    }

    @Transactional
    public void deleteDataById(Long vmsIfscId) {
         this.repo.deleteDataById(vmsIfscId);
    }

    // 정보 삭제-목록, 존재하는 데이터 만 삭제
    @Transactional
    public List<TbVmsIfscCmtrGradDto> deleteByIds(List<TbVmsIfscCmtrGradKey> ids) {
        List<TbVmsIfscCmtrGradDto> result = new ArrayList<>();
        for (TbVmsIfscCmtrGradKey id : ids) {
            Optional<TbVmsIfscCmtrGrad> obj = this.repo.findById(id);
            if (obj.isPresent()) {
                this.repo.deleteById(id);
                result.add(obj.get().toDto());
            }
        }
        return result;
    }

    @Transactional
    public TbVmsIfscCmtrGradDto.TbVmsIfscCmtrGradAllUpdReq saveAllIfscCmtrGradInfo(TbVmsIfscCmtrGradDto.TbVmsIfscCmtrGradAllUpdReq req) {
        try {
            this.repo.deleteDataAll();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        this.repo.insertDataAll("1", req.getGrad1().getLwstsped(), req.getGrad1().getHghssped());
        this.repo.insertDataAll("2", req.getGrad2().getLwstsped(), req.getGrad2().getHghssped());
        this.repo.insertDataAll("3", req.getGrad3().getLwstsped(), req.getGrad3().getHghssped());

        return req;
    }
}
