package com.spring.domain.schedule.service;

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 com.spring.domain.schedule.dto.BatchJobHistoryResponse;
import com.spring.domain.schedule.dto.FindJobHistoryRequest;
import com.spring.domain.schedule.repository.BatchJobExecutionRepository;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class FindJobHistoryService {

    private final BatchJobExecutionRepository batchJobExecutionRepository;

    @Transactional(readOnly = true)
    public List<BatchJobHistoryResponse> getJobHistory(FindJobHistoryRequest request, int page, int size) {
        return batchJobExecutionRepository.findAll(request.toPredicate(), PageRequest.of(page, size)).stream()
                .map(BatchJobHistoryResponse::fromEntity)
                .collect(Collectors.toList());
    }

}
