using System.Net;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using StellaOps.Feedser.Source.Acsc.Configuration;
using StellaOps.Feedser.Source.Common.Http;
using Xunit;

namespace StellaOps.Feedser.Source.Acsc.Tests.Acsc;

public sealed class AcscHttpClientConfigurationTests
{
    [Fact]
    public void AddAcscConnector_ConfiguresHttpClientOptions()
    {
        var services = new ServiceCollection();
        services.AddAcscConnector(options =>
        {
            options.BaseEndpoint = new Uri("https://origin.example/");
            options.RelayEndpoint = new Uri("https://relay.example/");
            options.RequestTimeout = TimeSpan.FromSeconds(42);
            options.Feeds.Clear();
            options.Feeds.Add(new AcscFeedOptions
            {
                Slug = "alerts",
                RelativePath = "/feeds/alerts/rss",
                Enabled = true,
            });
        });

        var provider = services.BuildServiceProvider();
        var monitor = provider.GetRequiredService<IOptionsMonitor<SourceHttpClientOptions>>();
        var options = monitor.Get(AcscOptions.HttpClientName);

        Assert.Equal("StellaOps/Feedser (+https://stella-ops.org)", options.UserAgent);
        Assert.Equal(HttpVersion.Version20, options.RequestVersion);
        Assert.Equal(HttpVersionPolicy.RequestVersionOrLower, options.VersionPolicy);
        Assert.Equal(TimeSpan.FromSeconds(42), options.Timeout);
        Assert.Contains("origin.example", options.AllowedHosts, StringComparer.OrdinalIgnoreCase);
        Assert.Contains("relay.example", options.AllowedHosts, StringComparer.OrdinalIgnoreCase);
        Assert.Equal("application/rss+xml, application/atom+xml;q=0.9, application/xml;q=0.8, text/xml;q=0.7", options.DefaultRequestHeaders["Accept"]);
    }
}
