using Hangfire;
using Microsoft.Extensions.DependencyInjection;

namespace KArtSell.Modules.ModelOperations.Scheduling;

public static class ModelOperationsScheduler
{
    public const string DispatcherJobId = "model-operations-dispatcher-v1";

    public static void RegisterModelOperationsSchedules(
        this IServiceProvider services,
        bool dispatcherEnabled,
        string dispatcherCron)
    {
        ArgumentException.ThrowIfNullOrWhiteSpace(dispatcherCron);

        var manager = services.GetRequiredService<IRecurringJobManager>();
        if (!dispatcherEnabled)
        {
            manager.RemoveIfExists(DispatcherJobId);
            return;
        }

        manager.AddOrUpdate<ModelOperationsDispatcherJob>(
            DispatcherJobId,
            job => job.ExecuteAsync(),
            dispatcherCron,
            new RecurringJobOptions { TimeZone = TimeZoneInfo.Utc });
    }
}
