using Microsoft.Extensions.DependencyInjection;
using Quartz;

namespace MktXService.Infrastructure.BackgroundJobs;

/// <summary>
/// EN: Extension methods for configuring Quartz.NET background jobs.
/// VI: Các phương thức mở rộng để cấu hình Quartz.NET background jobs.
/// </summary>
public static class QuartzServiceExtensions
{
    /// <summary>
    /// EN: Add all background jobs with Quartz.NET.
    /// VI: Thêm tất cả background jobs với Quartz.NET.
    /// </summary>
    public static IServiceCollection AddMktXBackgroundJobs(
        this IServiceCollection services,
        BackgroundJobOptions? options = null)
    {
        options ??= new BackgroundJobOptions();

        services.AddQuartz(q =>
        {
            if (options.EnableCampaignScheduler)
            {
                CampaignSchedulerJobSetup.ConfigureJob(q);
            }

            if (options.EnableMessageProcessor)
            {
                MessageProcessorJobSetup.ConfigureJob(q);
            }

            if (options.EnableWebhookProcessor)
            {
                WebhookEventProcessorJobSetup.ConfigureJob(q);
            }
        });

        services.AddQuartzHostedService(q => q.WaitForJobsToComplete = true);

        return services;
    }
}

/// <summary>
/// EN: Options for configuring background jobs.
/// VI: Tùy chọn để cấu hình background jobs.
/// </summary>
public class BackgroundJobOptions
{
    /// <summary>
    /// EN: Enable campaign scheduler job (default: true).
    /// VI: Bật job lập lịch chiến dịch (mặc định: true).
    /// </summary>
    public bool EnableCampaignScheduler { get; set; } = true;

    /// <summary>
    /// EN: Enable message processor job (default: true).
    /// VI: Bật job xử lý tin nhắn (mặc định: true).
    /// </summary>
    public bool EnableMessageProcessor { get; set; } = true;

    /// <summary>
    /// EN: Enable webhook event processor job (default: true).
    /// VI: Bật job xử lý webhook events (mặc định: true).
    /// </summary>
    public bool EnableWebhookProcessor { get; set; } = true;
}
