using FluentAssertions;
using StellaOps.Excititor.Worker.Options;
using StellaOps.Excititor.Worker.Scheduling;
using Xunit;

namespace StellaOps.Excititor.Worker.Tests;

public sealed class VexWorkerOptionsTests
{
    [Fact]
    public void ResolveSchedules_UsesDefaultIntervalWhenNotSpecified()
    {
        var options = new VexWorkerOptions
        {
            DefaultInterval = TimeSpan.FromMinutes(30),
            OfflineInterval = TimeSpan.FromHours(6),
            OfflineMode = false,
        };
        options.Providers.Add(new VexWorkerProviderOptions { ProviderId = "excititor:redhat" });

        var schedules = options.ResolveSchedules();

        schedules.Should().ContainSingle();
        schedules[0].Interval.Should().Be(TimeSpan.FromMinutes(30));
    }

    [Fact]
    public void ResolveSchedules_HonorsOfflineInterval()
    {
        var options = new VexWorkerOptions
        {
            DefaultInterval = TimeSpan.FromMinutes(30),
            OfflineInterval = TimeSpan.FromHours(8),
            OfflineMode = true,
        };
        options.Providers.Add(new VexWorkerProviderOptions { ProviderId = "excititor:offline" });

        var schedules = options.ResolveSchedules();

        schedules.Should().ContainSingle();
        schedules[0].Interval.Should().Be(TimeSpan.FromHours(8));
    }

    [Fact]
    public void ResolveSchedules_SkipsDisabledProviders()
    {
        var options = new VexWorkerOptions();
        options.Providers.Add(new VexWorkerProviderOptions { ProviderId = "excititor:enabled" });
        options.Providers.Add(new VexWorkerProviderOptions { ProviderId = "excititor:disabled", Enabled = false });

        var schedules = options.ResolveSchedules();

        schedules.Should().HaveCount(1);
        schedules[0].ProviderId.Should().Be("excititor:enabled");
    }

    [Fact]
    public void ResolveSchedules_UsesProviderIntervalOverride()
    {
        var options = new VexWorkerOptions
        {
            DefaultInterval = TimeSpan.FromMinutes(15),
        };
        options.Providers.Add(new VexWorkerProviderOptions
        {
            ProviderId = "excititor:custom",
            Interval = TimeSpan.FromMinutes(5),
            InitialDelay = TimeSpan.FromSeconds(10),
        });

        var schedules = options.ResolveSchedules();

        schedules.Should().ContainSingle();
        schedules[0].Interval.Should().Be(TimeSpan.FromMinutes(5));
        schedules[0].InitialDelay.Should().Be(TimeSpan.FromSeconds(10));
    }
}
