package gc.mda.signal_batch.batch.job;

import gc.mda.signal_batch.batch.listener.JobCompletionListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersValidator;
import org.springframework.batch.core.job.DefaultJobParametersValidator;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Slf4j
@Configuration
@Profile("!query")  // query 프로파일에서는 배치 작업 비활성화
@RequiredArgsConstructor
@ConditionalOnProperty(name = "vessel.batch.scheduler.enabled", havingValue = "true", matchIfMissing = true)
public class HourlyAggregationJobConfig {
    
    private final JobRepository jobRepository;
    private final HourlyAggregationStepConfig hourlyAggregationStepConfig;
    private final VesselStaticStepConfig vesselStaticStepConfig;
    private final JobCompletionListener jobCompletionListener;
    
    @Bean
    public Job hourlyAggregationJob() {
        return new JobBuilder("hourlyAggregationJob", jobRepository)
                .incrementer(new RunIdIncrementer())
                .validator(hourlyJobParametersValidator())
                .listener(jobCompletionListener)
                .start(hourlyAggregationStepConfig.mergeHourlyTracksStep())
                .next(hourlyAggregationStepConfig.gridHourlySummaryStep())
                .next(hourlyAggregationStepConfig.areaHourlySummaryStep())
                .next(vesselStaticStepConfig.vesselStaticSyncStep())
                .build();
    }
    
    @Bean
    public JobParametersValidator hourlyJobParametersValidator() {
        DefaultJobParametersValidator validator = new DefaultJobParametersValidator();
        validator.setRequiredKeys(new String[]{"startTime", "endTime", "timeBucket"});
        validator.setOptionalKeys(new String[]{"executionTime", "enableAbnormalDetection"});
        return validator;
    }
}
