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

namespace NeonSprawl.Server.Tests.Game.Interaction;

public class InteractablesWorldApiTests
{
    [Fact]
    public async Task GetInteractables_ShouldReturnOrderedDescriptors_WithSchemaV1()
    {
        // Arrange

        await using var factory = new InMemoryWebApplicationFactory();
        var client = factory.CreateClient();
        // Act

        var response = await client.GetAsync("/game/world/interactables");
        // Assert

        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        var body = await response.Content.ReadFromJsonAsync<InteractablesListResponse>();
        Assert.NotNull(body);
        Assert.Equal(InteractablesListResponse.CurrentSchemaVersion, body!.SchemaVersion);
        Assert.NotNull(body.Interactables);
        Assert.True(body.Interactables.Count >= 2);

        var ids = body.Interactables.Select(static x => x.InteractableId).ToList();
        Assert.Equal(ids.OrderBy(static x => x, StringComparer.Ordinal).ToList(), ids);

        var alpha = body.Interactables.Single(x => x.InteractableId == PrototypeInteractableRegistry.PrototypeResourceNodeAlphaId);
        Assert.Equal("resource_node", alpha.Kind);
        Assert.Equal(12, alpha.Anchor.X);
        Assert.Equal(0.5, alpha.Anchor.Y);
        Assert.Equal(-6, alpha.Anchor.Z);
        Assert.Equal(3.0, alpha.InteractionRadius);

        var term = body.Interactables.Single(x => x.InteractableId == PrototypeInteractableRegistry.PrototypeTerminalId);
        Assert.Equal("terminal", term.Kind);
        Assert.Equal(0, term.Anchor.X);
        Assert.Equal(0.5, term.Anchor.Y);
        Assert.Equal(0, term.Anchor.Z);
        Assert.Equal(3.0, term.InteractionRadius);
    }
}
