package com.its.api.its.service.road;

import com.its.api.its.model.dto.road.TbRoadIfscRltnDto;
import com.its.api.its.model.entity.road.TbRoadIfscRltn;
import com.its.api.its.model.entity.road.TbRoadIfscRltnKey;
import com.its.api.its.repository.road.TbRoadIfscRltnRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@Slf4j
@RequiredArgsConstructor
@Service
public class TbRoadIfscRltnService {

    private final TbRoadIfscRltnRepository repo;

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

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

    // 데이터 1건 조회(기존 데이터가 반드시 존재해야 함)
    @Transactional(readOnly = true)
    public TbRoadIfscRltnDto findById(TbRoadIfscRltnKey id) {
        TbRoadIfscRltn entity = requireOne(id);
        return entity.toDto();
    }

    // 데이터 변경
    /*public TbRoadIfscRltnDto updateById(TbRoadIfscRltnKey id, TbRoadIfscRltnDto.TbRoadIfscRltnUpdReq req) {
        TbRoadIfscRltn entity = requireOne(id);
        entity.updateInfo(req);
        this.repo.save(entity);
        return entity.toDto();
    }*/

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

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

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

    // 정보제공구간 구성정보 조회
    @Transactional(readOnly = true)
    public List<TbRoadIfscRltnDto> findIfscRltnById(Long roadId) {
        List<TbRoadIfscRltnDto> result = new ArrayList<>();
        List<TbRoadIfscRltn> data = this.repo.findIfscRltnById(roadId);
        for (TbRoadIfscRltn obj : data) {
            result.add(obj.toDto());
        }
        return result;
    }

    @Transactional
    protected boolean updateIfscRltnData(Long roadId, List<TbRoadIfscRltnDto.TbRoadIfscRltnUpdReq> reqList) {
        // 기존 데이터를 모두 삭제
        this.repo.deleteDataById(roadId);

        // 데이터 다시 입력
        for (TbRoadIfscRltnDto.TbRoadIfscRltnUpdReq req : reqList) {
            TbRoadIfscRltn obj = req.toEntity(roadId);
            this.repo.save(obj);
        }
        return true;
    }

    public List<TbRoadIfscRltnDto> updateIfscRltn(Long roadId, List<TbRoadIfscRltnDto.TbRoadIfscRltnUpdReq> reqList) {
        updateIfscRltnData(roadId, reqList);
        return findIfscRltnById(roadId);
    }
}
