using System.Net;
using System.Net.Http.Json;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using NeonSprawl.Server.Game.Combat;
using NeonSprawl.Server.Game.Targeting;
using Xunit;

namespace NeonSprawl.Server.Tests.Game.Combat;

public sealed class CombatTargetFixtureApiTests
{
    private const string FixturePath = "/game/__dev/combat-targets-fixture";

    [Fact]
    public async Task PostCombatTargetFixture_ShouldResetPrototypeTargets_WhenEnabled()
    {
        // Arrange
        await using var factory = new InMemoryWebApplicationFactory();
        var client = factory.CreateClient();
        var store = factory.Services.GetRequiredService<ICombatEntityHealthStore>();
        _ = store.TryApplyDamage(PrototypeTargetRegistry.PrototypeTargetAlphaId, 100, out _);

        // Act
        var response = await client.PostAsJsonAsync(
            FixturePath,
            new CombatTargetFixtureRequest
            {
                SchemaVersion = CombatTargetFixtureRequest.CurrentSchemaVersion,
            });

        // Assert
        Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        var body = await response.Content.ReadFromJsonAsync<CombatTargetFixtureResponse>();
        Assert.NotNull(body);
        Assert.True(body!.Applied);
        store.TryGet(PrototypeTargetRegistry.PrototypeTargetAlphaId, out var alpha);
        Assert.Equal(PrototypeCombatConstants.MaxPrototypeTargetHp, alpha.CurrentHp);
        Assert.False(alpha.Defeated);
    }

    [Fact]
    public async Task PostCombatTargetFixture_ShouldReturnNotFound_WhenRouteNotRegistered()
    {
        // Arrange
        await using var factory = new InMemoryWebApplicationFactory();
        var client = factory.WithWebHostBuilder(b =>
        {
            b.UseEnvironment("Production");
            b.UseSetting("Game:EnableCombatTargetFixtureApi", "false");
        }).CreateClient();

        // Act
        var response = await client.PostAsJsonAsync(
            FixturePath,
            new CombatTargetFixtureRequest
            {
                SchemaVersion = CombatTargetFixtureRequest.CurrentSchemaVersion,
            });

        // Assert
        Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
    }
}
