package com.iqser.red.service.persistence.management.v1.processor.jobs;

import java.text.ParseException;

import com.iqser.red.service.persistence.management.v1.processor.service.job.*;

import org.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CreateJobsConfiguration {

    @Bean
    public Trigger notificationEmailSummaryJobTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(notificationEmailSummaryJob())
                .withIdentity("SendNotificationEmailJobTrigger")
                .withDescription("Triggers SendNotificationEmailJob every night at 3 AM")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0 0 3 * * ?")))
                .build();
    }


    @Bean
    public JobDetail notificationEmailSummaryJob() {

        return JobBuilder.newJob()
                .ofType(SendNotificationEmailJob.class)
                .storeDurably()
                .withIdentity("SendNotificationEmailJob")
                .withDescription("Send summary email with daily/weekly notifications")
                .build();
    }


    @Bean
    public Trigger automaticAnalysisJobTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(automaticAnalysisJobDetail())
                .withIdentity("AutomaticAnalysisJobTrigger")
                .withDescription("Triggers AutomaticAnalysisJob every 10 seconds")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("*/10 * * * * ?")))
                .build();
    }


    @Bean
    public JobDetail automaticAnalysisJobDetail() {

        return JobBuilder.newJob()
                .ofType(AutomaticAnalysisJob.class)
                .storeDurably()
                .withIdentity("AutomaticAnalysisJob")
                .withDescription("Automatically queues files for analysis")
                .build();
    }


    @Bean
    public Trigger softDeletedFilesCleanupJobTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(softDeletedFilesCleanupJobDetail())
                .withIdentity("SoftDeletedFilesCleanupJobTrigger")
                .withDescription("Triggers SoftDeletedFilesCleanupJob every 30 minutes")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0 */30 * * * ?")))
                .build();
    }


    @Bean
    public Trigger deletedFilesCleanupJobTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(deletedFilesCleanupJobDetail())
                .withIdentity("DeletedFilesCleanupJobTrigger")
                .withDescription("Triggers DeletedFilesCleanupJob every night at 2 AM")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0 0 2 * * ?")))
                .build();
    }


    @Bean
    public Trigger documentDataFilesConversionJobTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(documentDataFilesConversionJobDetail())
                .withIdentity("DocumentDataFilesConversionJobTrigger")
                .withDescription("Triggers DocumentDataFilesConversionJob nightly at 2 AM and runs for 2 hours")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0 0 2 * * ?")))
                .build();
    }


    @Bean
    public JobDetail softDeletedFilesCleanupJobDetail() {

        return JobBuilder.newJob()
                .ofType(SoftDeletedFilesCleanupJob.class)
                .storeDurably()
                .withIdentity("SoftDeletedFilesCleanupJob")
                .withDescription("Hard delete soft-deleted dossiers / files after certain time")
                .build();
    }


    @Bean
    public JobDetail deletedFilesCleanupJobDetail() {

        return JobBuilder.newJob()
                .ofType(DeletedFilesCleanupJob.class)
                .storeDurably()
                .withIdentity("DeletedFilesCleanupJob")
                .withDescription("Delete file data and update indices of hard deleted files after certain time")
                .build();
    }


    @Bean
    public JobDetail documentDataFilesConversionJobDetail() {

        return JobBuilder.newJob()
                .ofType(DocumentDataFilesConversionJob.class)
                .storeDurably()
                .withIdentity("DocumentDataFilesConversionJob")
                .withDescription("Convert document data files from json to protobuf format")
                .build();
    }


    @Bean
    public Trigger keyCloakUserSyncJobTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(keyCloakUserSyncJobDetail())
                .withIdentity("KeyCloakUserSyncJobTrigger")
                .withDescription("Triggers KeyCloakUserSyncJob every 2 minutes")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0 */2 * * * ?")))
                .build();
    }


    @Bean
    public JobDetail keyCloakUserSyncJobDetail() {

        return JobBuilder.newJob()
                .ofType(KeyCloakUserSyncJob.class)
                .storeDurably()
                .withIdentity("KeyCloakUserSyncJob")
                .withDescription("Sync KC Users in case of deletions in KC")
                .build();
    }


    @Bean
    public Trigger syncUserPermissionsJobTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(syncUserPermissionsJobDetail())
                .withIdentity("SyncUserPermissionsJobTrigger")
                .withDescription("Triggers SyncUserPermissionsJob every 2 minutes")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0 */2 * * * ?")))
                .build();
    }


    @Bean
    public JobDetail syncUserPermissionsJobDetail() {

        return JobBuilder.newJob()
                .ofType(SyncUserPermissionsJob.class)
                .storeDurably()
                .withIdentity("SyncUserPermissionsJob")
                .withDescription("Sync custom permissions for ACL")
                .build();
    }


    @Bean
    public Trigger downloadCleanupJobTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(downloadCleanupJobDetail())
                .withIdentity("DownloadCleanupJobTrigger")
                .withDescription("Triggers DownloadCleanupJob every 30 minutes")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0 */30 * * * ?")))
                .build();
    }


    @Bean
    public JobDetail downloadCleanupJobDetail() {

        return JobBuilder.newJob()
                .ofType(DownloadCleanupJob.class)
                .storeDurably()
                .withIdentity("DownloadCleanupJob")
                .withDescription("Hard delete dossiers / files after certain time")
                .build();
    }


    @Bean
    public Trigger downloadReadyJobTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(downloadReadyJobDetail())
                .withIdentity("DownloadReadyJobTrigger")
                .withDescription("Triggers DownloadReadyJob every 10 seconds")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("*/10 * * * * ?")))
                .build();
    }


    @Bean
    public JobDetail downloadReadyJobDetail() {

        return JobBuilder.newJob()
                .ofType(DownloadReadyJob.class)
                .storeDurably()
                .withIdentity("DownloadReadyJob")
                .withDescription("Builds the download package if all parallel processed files are ready")
                .build();
    }


    @Bean
    public Trigger analysisFlagCalculationSchedulerTrigger() throws ParseException {

        return TriggerBuilder.newTrigger()
                .forJob(analysisFlagCalculationSchedulerJobDetail())
                .withIdentity("AnalysisFlagCalculationSchedulerTrigger")
                .withDescription("Triggers AnalysisFlagCalculationSchedulerJob every 10 seconds")
                .withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("*/10 * * * * ?")))
                .build();
    }


    @Bean
    public JobDetail analysisFlagCalculationSchedulerJobDetail() {

        return JobBuilder.newJob()
                .ofType(AnalysisFlagCalculationSchedulerJob.class)
                .storeDurably()
                .withIdentity("AnalysisFlagCalculationSchedulerJob")
                .withDescription("Automatically queues files for analysis flag calculation")
                .build();
    }

}
