using System.Net;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using UniVerse.Api.Tests.Helpers;
using Xunit;

namespace UniVerse.Api.Tests.RateLimiting;

public class RateLimitingTests
{
    [Fact]
    public async Task GlobalRateLimiter_Returns429_WhenPartitionExceedsLimit()
    {
        await using var factory = new ApiWebApplicationFactory()
            .WithWebHostBuilder(builder =>
            {
                builder.ConfigureAppConfiguration((_, config) =>
                {
                    config.AddInMemoryCollection(new Dictionary<string, string?>
                    {
                        ["RateLimiting:PermitLimit"] = "1",
                        ["RateLimiting:WindowSeconds"] = "60",
                        ["RateLimiting:QueueLimit"] = "0"
                    });
                });
            });

        using var client = factory.CreateClient();
        client.DefaultRequestHeaders.Add("Authorization", TestJwtFactory.BearerHeader("Student"));

        using var firstResponse = await client.GetAsync("api/v1/tags");
        using var secondResponse = await client.GetAsync("api/v1/tags");

        Assert.NotEqual(HttpStatusCode.TooManyRequests, firstResponse.StatusCode);
        Assert.Equal(HttpStatusCode.TooManyRequests, secondResponse.StatusCode);
    }
}
