package com.interplug.qcast.batch.estimate;

import com.interplug.qcast.biz.estimate.EstimateService;
import com.interplug.qcast.biz.estimate.dto.EstimateSyncResponse;
import com.interplug.qcast.biz.estimate.dto.PlanSyncResponse;
import com.interplug.qcast.util.InterfaceQsp;
import java.util.Collections;
import java.util.List;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.transaction.PlatformTransactionManager;

/** Plan 확정 Item 마스터 동기화 배치 */
@Configuration
public class EstimateSyncConfiguration implements JobExecutionListener {
  private final EstimateService estimateService;

  private final InterfaceQsp interfaceQsp;

  EstimateSyncResponse estimateSyncResponse;

  @Value("${qsp.estimate-sync-batch-url}")
  private String qspInterfaceUrl;

  public EstimateSyncConfiguration(EstimateService estimateService, InterfaceQsp interfaceQsp) {
    this.estimateService = estimateService;
    this.interfaceQsp = interfaceQsp;
  }

  @Bean
  public Job estimateSyncJob(JobRepository jobRepository, Step estimateSyncStep) {
    return new JobBuilder("estimateSyncJob", jobRepository).start(estimateSyncStep).build();
  }

  @Bean
  public Step estimateSyncStep(
      JobRepository jobRepository, PlatformTransactionManager transactionManager) throws Exception {
    return new StepBuilder("estimateSyncStep", jobRepository)
        .<PlanSyncResponse, PlanSyncResponse>chunk(100, transactionManager)
        .reader(estimateSyncReader())
        .processor(estimateSyncProcessor())
        .writer(estimateSyncWriter())
        .build();
  }

  @Bean
  @StepScope
  public ListItemReader<PlanSyncResponse> estimateSyncReader() throws Exception {
    this.estimateSyncResponse =
        interfaceQsp.callApiData(
            HttpMethod.POST,
            qspInterfaceUrl,
            estimateService.selectEstimateSyncFailList(),
            EstimateSyncResponse.class);
    return (estimateSyncResponse != null)
        ? new ListItemReader<>(estimateSyncResponse.getSuccessList())
        : new ListItemReader<>(Collections.emptyList());
  }

  @Bean
  public ItemProcessor<PlanSyncResponse, PlanSyncResponse> estimateSyncProcessor() {
    return item -> item;
  }

  @Bean
  public ItemWriter<PlanSyncResponse> estimateSyncWriter() {
    return items -> {
      estimateService.setEstimateSyncSave((List<PlanSyncResponse>) items.getItems());
    };
  }
}
