using RolemasterDb.App.Frontend.AppState;

namespace RolemasterDb.ImportTool.Tests;

public sealed class TableContextUrlSerializerTests
{
    private readonly TableContextUrlSerializer serializer = new();

    [Fact]
    public void Build_relative_uri_round_trips_all_supported_context_fields()
    {
        var snapshot = new TableContextSnapshot(TableSlug: "void", GroupKey: "alpha", ColumnKey: "E", RollBand: "16-20", RollJump: 18, ResultId: 11617, Mode: TableContextMode.Curation, QueueScope: "pinned");

        var uri = serializer.BuildRelativeUri("/curation", snapshot);
        var roundTripped = serializer.Parse(new Uri(new Uri("https://example.test"), uri).AbsoluteUri);

        Assert.Equal("/curation?table=void&group=alpha&column=E&rollBand=16-20&roll=18&result=11617&mode=curation&scope=pinned", uri);
        Assert.Equal(snapshot, roundTripped);
    }

    [Fact]
    public void Build_relative_uri_omits_empty_context_values()
    {
        var snapshot = new TableContextSnapshot(TableSlug: "slash", GroupKey: "", ColumnKey: null, RollBand: "01-05", RollJump: null, ResultId: null, Mode: TableContextMode.Reference, QueueScope: " ");

        var uri = serializer.BuildRelativeUri("/tables", snapshot);

        Assert.Equal("/tables?table=slash&rollBand=01-05&mode=reference", uri);
    }

    [Fact]
    public void Parse_treats_unknown_mode_as_null_without_losing_other_context()
    {
        var snapshot = serializer.Parse("https://example.test/tools/diagnostics?table=arcane-nether&column=C&rollBand=06-10&result=9748&mode=unknown&scope=selected-table");

        Assert.Equal("arcane-nether", snapshot.TableSlug);
        Assert.Equal("C", snapshot.ColumnKey);
        Assert.Equal("06-10", snapshot.RollBand);
        Assert.Equal(9748, snapshot.ResultId);
        Assert.Null(snapshot.Mode);
        Assert.Equal("selected-table", snapshot.QueueScope);
    }
}