using System.Net.Http.Json;
using System.Text.Json;
using GameList.Tests.Support;

namespace GameList.Tests;

public class ResultsTests
{
    [Fact]
    public async Task Results_available_after_admin_unlocks()
    {
        await using var factory = new TestWebApplicationFactory();
        var admin = factory.CreateClientWithCookies();
        await admin.RegisterAsync("admin", admin: true);

        var player = factory.CreateClientWithCookies();
        await player.RegisterAsync("player");
        var suggestionId = await player.CreateSuggestionAsync("ResultGame");

        await player.PostAsJsonAsync("/api/me/phase/next", new { });
        await player.PostAsJsonAsync("/api/votes", new
        {
            SuggestionId = suggestionId,
            Score = 8
        });
        await player.PostAsJsonAsync("/api/votes/finalize", new { Final = true });

        await admin.PostAsJsonAsync("/api/admin/results", new { resultsOpen = true });

        await player.PostAsJsonAsync("/api/me/phase/next", new { });

        var results = await player.GetFromJsonAsync<JsonElement>("/api/results");

        Assert.True(results.GetArrayLength() >= 1);
        var first = results[0];
        Assert.Equal("ResultGame", first.GetProperty("name").GetString());
        Assert.Equal(8, (int)first.GetProperty("average").GetDouble());
    }

    [Fact]
    public async Task Results_locked_returns_error()
    {
        await using var factory = new TestWebApplicationFactory();
        var client = factory.CreateClientWithCookies();
        await client.RegisterAsync("user");
        await client.PostAsJsonAsync("/api/me/phase/next", new { });
        var resp = await client.GetAsync("/api/results");
        Assert.Equal(System.Net.HttpStatusCode.BadRequest, resp.StatusCode);
    }

    [Fact]
    public async Task Results_require_results_phase_and_auth()
    {
        await using var factory = new TestWebApplicationFactory();
        var admin = factory.CreateClientWithCookies();
        await admin.RegisterAsync("admin", admin: true);
        var player = factory.CreateClientWithCookies();
        await player.RegisterAsync("player");
        await admin.PostAsJsonAsync("/api/admin/results", new { resultsOpen = true });

        var anon = factory.CreateClient();
        var unauthorized = await anon.GetAsync("/api/results");
        Assert.Equal(System.Net.HttpStatusCode.Unauthorized, unauthorized.StatusCode);

        var ok = await player.GetAsync("/api/results");
        Assert.Equal(System.Net.HttpStatusCode.OK, ok.StatusCode);
    }

    [Fact]
    public async Task Results_payload_contains_fields_and_ordering()
    {
        await using var factory = new TestWebApplicationFactory();
        var admin = factory.CreateClientWithCookies();
        await admin.RegisterAsync("admin", admin: true);

        var player = factory.CreateClientWithCookies();
        await player.RegisterAsync("player");
        var s1 = await player.CreateSuggestionAsync("High");
        _ = await player.CreateSuggestionAsync("NoVotes");

        await player.PostAsJsonAsync("/api/me/phase/next", new { });
        await player.PostAsJsonAsync("/api/votes", new
        {
            SuggestionId = s1,
            Score = 9
        });
        await player.PostAsJsonAsync("/api/votes/finalize", new { Final = true });

        await admin.PostAsJsonAsync("/api/admin/results", new { resultsOpen = true });
        await player.PostAsJsonAsync("/api/me/phase/next", new { });

        var results = await player.GetFromJsonAsync<List<JsonElement>>("/api/results");
        Assert.NotNull(results);
        Assert.Equal(2, results.Count);
        Assert.Equal("High", results[0].GetProperty("name").GetString());
        Assert.Equal(9, (int)results[0].GetProperty("average").GetDouble());
        Assert.Equal(1, results[0].GetProperty("count").GetInt32());
        Assert.Equal(0, results[1].GetProperty("average").GetDouble());
    }
}
