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.AdminUserSyncResponse;
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 AdminUserConfiguration implements JobExecutionListener {
  private final UserService userService;

  private final InterfaceQsp interfaceQsp;

  List<AdminUserSyncResponse> adminUserSyncList;

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

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

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

  @Bean
  public Step adminUserStep(
      JobRepository jobRepository, PlatformTransactionManager transactionManager) throws Exception {
    return new StepBuilder("adminUserStep", jobRepository)
        .<AdminUserSyncResponse, AdminUserSyncResponse>chunk(100, transactionManager)
        .reader(adminUserReader())
        .processor(adminUserProcessor())
        .writer(adminUserWriter())
        .build();
  }

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

  @Bean
  public ItemProcessor<AdminUserSyncResponse, AdminUserSyncResponse> adminUserProcessor() {
    return item -> item;
  }

  @Bean
  public ItemWriter<AdminUserSyncResponse> adminUserWriter() {
    return items -> {
      userService.setAdminUserSyncSave((List<AdminUserSyncResponse>) items.getItems());
    };
  }
}
