package com.its.api.its.service.link;

import com.its.api.its.model.dto.analysis.TbClctSystSttsHsClctDto;
import com.its.api.its.model.dto.analysis.TbClctSystSttsHsClctInf;
import com.its.api.its.model.dto.analysis.TbClctSystSttsHsPrcnDto;
import com.its.api.its.model.dto.analysis.TbClctSystSttsHsPrcnInf;
import com.its.api.its.model.dto.link.TbClctSystSttsHsDto;
import com.its.api.its.model.entity.link.TbClctSystSttsHs;
import com.its.api.its.model.entity.link.TbClctSystSttsHsKey;
import com.its.api.its.repository.link.TbClctSystSttsHsRepository;
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 TbClctSystSttsHsService {

    private final TbClctSystSttsHsRepository repo;

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

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

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

    /**
     * 04.분석-1.교통정보수집결과분석
     * @param fromDt
     * @param toDt
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbClctSystSttsHsClctDto> findAllClctSystByDateRange(String fromDt, String toDt) {
        List<TbClctSystSttsHsClctDto> result = new ArrayList<>();
        List<TbClctSystSttsHsClctInf> data = this.repo.findAllClctSystByDateRange(fromDt, toDt);
        if (data != null) {
            data.forEach(obj -> {
                TbClctSystSttsHsClctDto dto = TbClctSystSttsHsClctDto.builder()
                        .prcnDt(obj.getPrcnDt())
                        .oper01(obj.getOper01().intValue())
                        .moct01(obj.getMoct01().intValue())
                        .utis01(obj.getUtis01().intValue())
                        .utis02(obj.getUtis02().intValue())
                        .vds01(obj.getVds01().intValue())
                        .build();
                dto.makeClctTot();
                result.add(dto);
            });
        }
        return result;
    }


    /**
     * 04.분석-2.교통정보가공결과분석
     * @param fromDt
     * @param toDt
     * @return
     */
    @Transactional(readOnly = true)
    public List<TbClctSystSttsHsPrcnDto> findAllPrcsSystByDateRange(String fromDt, String toDt) {
        List<TbClctSystSttsHsPrcnDto> result = new ArrayList<>();
        List<TbClctSystSttsHsPrcnInf> data = this.repo.findAllPrcnSystByDateRange(fromDt, toDt);
        if (data != null) {
            data.forEach(obj -> {
                TbClctSystSttsHsPrcnDto dto = TbClctSystSttsHsPrcnDto.builder()
                        .prcnDt(obj.getPrcnDt())
                        .oper01(obj.getOper01().intValue())
                        .moct01(obj.getMoct01().intValue())
                        .utis01(obj.getUtis01().intValue())
                        .utis02(obj.getUtis02().intValue())
                        .vds01(obj.getVds01().intValue())
                        .missNum(obj.getMissNum().intValue())
                        .adjsNum(obj.getAdjsNum().intValue())
                        .smthNum(obj.getSmthNum().intValue())
                        .build();
                dto.makeClctTot();
                result.add(dto);
            });
        }
        return result;
    }
}
