using System.Linq;
using System.Net;
using System.Net.Http.Json;
using NeonSprawl.Server.Game.Gathering;
using Xunit;

namespace NeonSprawl.Server.Tests.Game.Gathering;

public class ResourceNodeDefinitionsWorldApiTests
{
    private static readonly string[] FrozenFourInIdOrder =
    [
        "prototype_bio_mat_gamma",
        "prototype_resource_node_alpha",
        "prototype_subsurface_vein_beta",
        "prototype_urban_bulk_delta",
    ];

    [Fact]
    public async Task GetResourceNodeDefinitions_ShouldReturnSchemaV1_WithFrozenFourInIdOrder()
    {
        // Arrange
        await using var factory = new InMemoryWebApplicationFactory();
        var client = factory.CreateClient();
        // Act
        var response = await client.GetAsync("/game/world/resource-node-definitions");
        // Assert
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        var body = await response.Content.ReadFromJsonAsync<ResourceNodeDefinitionsListResponse>();
        Assert.NotNull(body);
        Assert.Equal(ResourceNodeDefinitionsListResponse.CurrentSchemaVersion, body!.SchemaVersion);
        Assert.NotNull(body.Nodes);
        var ids = body.Nodes.Select(static n => n.Id).ToList();
        Assert.Equal(FrozenFourInIdOrder, ids);

        var alpha = body.Nodes.Single(n => n.Id == "prototype_resource_node_alpha");
        Assert.Equal("Prototype Salvage Heap", alpha.DisplayName);
        Assert.Equal("consumer_salvage", alpha.GatherLens);
        Assert.Equal(10, alpha.MaxGathers);
        Assert.Equal("scrap_metal_bulk", alpha.Yield.ItemId);
        Assert.Equal(1, alpha.Yield.Quantity);

        var delta = body.Nodes.Single(n => n.Id == "prototype_urban_bulk_delta");
        Assert.Equal("urban_bulk", delta.GatherLens);
        Assert.Equal(5, delta.Yield.Quantity);
    }
}
