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

import java.text.ParseException;

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;

import com.iqser.red.service.persistence.management.v1.processor.service.job.AutomaticAnalysisJob;
import com.iqser.red.service.persistence.management.v1.processor.service.job.DeletedFilesCleanupJob;
import com.iqser.red.service.persistence.management.v1.processor.service.job.DownloadCleanupJob;
import com.iqser.red.service.persistence.management.v1.processor.service.job.KeyCloakUserSyncJob;
import com.iqser.red.service.persistence.management.v1.processor.service.job.SendNotificationEmailJob;
import com.iqser.red.service.persistence.management.v1.processor.service.job.SyncUserPermissionsJob;

@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 deletedFilesCleanupJobTrigger() throws ParseException {

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


    @Bean
    public JobDetail deletedFilesCleanupJobDetail() {

        return JobBuilder.newJob()
                .ofType(DeletedFilesCleanupJob.class)
                .storeDurably()
                .withIdentity("DeletedFilesCleanupJob")
                .withDescription("Hard delete dossiers / files after certain time")
                .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();
    }

}
