package com.spring.domain.schedule.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.spring.domain.schedule.dto.BatchChartResponse;
import com.spring.domain.schedule.dto.BatchJobAverageDurationProjection;
import com.spring.domain.schedule.dto.BatchJobExecutionProjection;
import com.spring.domain.schedule.dto.BatchJobHourProjection;
import com.spring.domain.schedule.dto.BatchJobStatusCountProjection;
import com.spring.domain.schedule.repository.BatchJobExecutionRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class BatchChartService {
    
    private final BatchJobExecutionRepository batchJobExecutionRepository;

    @Transactional(readOnly = true)
    public BatchChartResponse getBatchJobExecutionData(int year, int month) {
        return new BatchChartResponse(
                getJobAverageDurations(year, month), 
                getJobStatusCounts(year, month),
                getHourlyJobExecutionDistribution(year, month),
                getJobExecutionSummary(year, month)
            );
    }

    private List<BatchJobAverageDurationProjection> getJobAverageDurations(int year, int month) {
        return batchJobExecutionRepository.findJobAverageDurations(year, month);
    }

    private List<BatchJobStatusCountProjection> getJobStatusCounts(int year, int month) {
        return batchJobExecutionRepository.findJobStatusCounts(year, month);
    }

    private List<BatchJobHourProjection> getHourlyJobExecutionDistribution(int year, int month) {
        return batchJobExecutionRepository.findHourlyJobExecutionDistribution(year, month);
    }

    private List<BatchJobExecutionProjection> getJobExecutionSummary(int year, int month) {
        return batchJobExecutionRepository.findJobExecutionSummaryDays(year, month);
    }

}
