package com.spring.domain.schedule.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.spring.domain.schedule.dto.BatchJobHistoryProjection;
import com.spring.domain.schedule.repository.BatchJobExecutionRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class FindJobHistoryService {

    private final BatchJobExecutionRepository batchJobExecutionRepository;

    @Transactional(readOnly = true)
    public List<BatchJobHistoryProjection> getJobHistory(LocalDateTime fromDate, LocalDateTime toDate, String jobName, int page, int size) {
        return batchJobExecutionRepository.findJobHistory(fromDate, toDate, PageRequest.of(page, size)).stream()
                .filter(jobHistory -> !StringUtils.hasText(jobName) || jobHistory.getJobName().equals(jobName))
                .collect(Collectors.toList());
    }

}
