package com.samsung.batch.excutor;

import com.samsung.batch.UserBatchService;
import jakarta.mail.MessagingException;
import lombok.extern.log4j.Log4j2;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Log4j2
public class UserBatchExecutor extends QuartzJobBean {

    @Value("${node-id}")
    private String nodeId;

    @Autowired
    private UserBatchService userBatchService;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        String jobName = jobDetail.getKey().getName();
        log.info(new StringBuilder()
                .append("nodeId : ").append(nodeId).append(", ")
                .append("jobName : ").append(jobName).append(", ")
                .append("description : ").append(jobDetail.getDescription())
        );
        switch (jobName) {
            case "batchUserLongTermCheck" -> userBatchService.execUserLongTermCheck();
            case "batchUserAuthExpired" -> userBatchService.execUserAuthValidCheck();
            case "batchUserAuthExpiredMailing" -> {
                try {
                    userBatchService.execUserAuthValidAlarmMail();
                } catch (IOException | MessagingException e) {
                    log.error(e.getMessage(), e);
                }
            }
            default -> {
            }
        }
    }
}
