using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StellaOps.DependencyInjection;
using StellaOps.Feedser.Core.Jobs;

namespace StellaOps.Feedser.Core.Tests;

public sealed class TestPluginRoutine : IDependencyInjectionRoutine
{
    public IServiceCollection Register(IServiceCollection services, IConfiguration configuration)
    {
        ArgumentNullException.ThrowIfNull(services);
        ArgumentNullException.ThrowIfNull(configuration);

        var builder = new JobSchedulerBuilder(services);
        var timeoutSeconds = configuration.GetValue<int?>("plugin:test:timeoutSeconds") ?? 30;

        builder.AddJob<PluginJob>(
            PluginJob.JobKind,
            cronExpression: "*/10 * * * *",
            timeout: TimeSpan.FromSeconds(timeoutSeconds),
            leaseDuration: TimeSpan.FromSeconds(5));

        services.AddSingleton<PluginRoutineExecuted>();
        return services;
    }
}

public sealed class PluginRoutineExecuted
{
}

public sealed class PluginJob : IJob
{
    public const string JobKind = "plugin:test";

    public Task ExecuteAsync(JobExecutionContext context, CancellationToken cancellationToken)
        => Task.CompletedTask;
}
