package com.its.api.utis.service;

import com.its.api.utis.model.dto.RseopstateDto;
import com.its.api.utis.model.entity.Rseopstate;
import com.its.api.utis.repository.RsemstRepository;
import com.its.api.utis.repository.RseopstateRepository;
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;

@Slf4j
@RequiredArgsConstructor
@Service
public class RseopstateService {

    private final RseopstateRepository repo;
    private final RsemstRepository rseRepo;

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

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

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

    /**
     * RSE 상태 통합
     * @return
     */
    /*@Transactional(readOnly = true)
    public RseopstateTotalDto findAllStateTotal() {

        int total = this.rseRepo.findAllCount();
        int normal = this.repo.findAllNormalCount();

        return RseopstateTotalDto.builder()
                .type("RSE")
                .total(total)
                .normal(normal)
                .error(total - normal)
                .build();
    }*/
}
