package com.its.op.service.its.scrs;

import com.its.op.dao.repository.its.scrs.TbScSgnlIxrMngmRepository;
import com.its.op.dto.its.scrs.TbScSgnlIxrMngmDto;
import com.its.op.entity.its.scrs.TbScSgnlIxrMngm;
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 TbScSgnlIxrMngmService {

    private final TbScSgnlIxrMngmRepository repo;

    // 데이터 1건 조회, 없으면 exception
    private TbScSgnlIxrMngm requireOne(Integer sgnlIxrNmbr) throws NoSuchElementException {
        Optional<TbScSgnlIxrMngm> info = this.repo.findById(sgnlIxrNmbr);
        if (info.isPresent()) {
            return info.get();
        }
        else {
            throw new NoSuchElementException("데이터가 존재하지 않습니다: " + sgnlIxrNmbr);
        }
    }

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

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

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

    // 데이터 변경
    @Transactional
    public TbScSgnlIxrMngmDto updateById(Integer sgnlIxrNmbr, TbScSgnlIxrMngmDto.TbScSgnlIxrMngmUpdReq req) {
        TbScSgnlIxrMngm entity = requireOne(sgnlIxrNmbr);
        entity.updateInfo(req);
        this.repo.save(entity);
        return entity.toDto();
    }

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

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

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

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

}
