package com.interplug.qcast.batch.system;

import com.fasterxml.jackson.core.type.TypeReference;
import com.interplug.qcast.biz.user.UserService;
import com.interplug.qcast.biz.user.dto.BusinessChargerSyncResponse;
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;

/** 영업사원 동기화 배치 */
@Configuration
public class BusinessChargerConfiguration implements JobExecutionListener {
  private final UserService userService;

  private final InterfaceQsp interfaceQsp;

  List<BusinessChargerSyncResponse> businessChargerSyncList;

  @Value("${qsp.master-business-charger-batch-url}")
  private String qspInterfaceUrl;

  public BusinessChargerConfiguration(UserService userService, InterfaceQsp interfaceQsp) {
    this.userService = userService;
    this.interfaceQsp = interfaceQsp;
  }

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

  @Bean
  public Step businessChargerStep(
      JobRepository jobRepository, PlatformTransactionManager transactionManager) throws Exception {
    return new StepBuilder("businessChargerStep", jobRepository)
        .<BusinessChargerSyncResponse, BusinessChargerSyncResponse>chunk(100, transactionManager)
        .reader(businessChargerReader())
        .processor(businessChargerProcessor())
        .writer(businessChargerWriter())
        .build();
  }

  @Bean
  @StepScope
  public ListItemReader<BusinessChargerSyncResponse> businessChargerReader() throws Exception {
    this.businessChargerSyncList =
        interfaceQsp.callApiData(
            HttpMethod.GET,
            qspInterfaceUrl,
            null,
            new TypeReference<List<BusinessChargerSyncResponse>>() {});
    return (businessChargerSyncList != null)
        ? new ListItemReader<>(businessChargerSyncList)
        : new ListItemReader<>(Collections.emptyList());
  }

  @Bean
  public ItemProcessor<BusinessChargerSyncResponse, BusinessChargerSyncResponse>
      businessChargerProcessor() {
    return item -> item;
  }

  @Bean
  public ItemWriter<BusinessChargerSyncResponse> businessChargerWriter() {
    return items -> {
      userService.setBusinessChargerSyncSave((List<BusinessChargerSyncResponse>) items.getItems());
    };
  }
}
