using RolemasterDb.App.Features;
using RolemasterDb.App.Frontend.AppState;

namespace RolemasterDb.ImportTool.Tests;

public sealed class TableContextCellResolverTests
{
    [Fact]
    public void Find_cell_prefers_result_id_and_falls_back_to_location_context()
    {
        var detail = CreateDetail(new CriticalTableCellDetail(11, "01-05", "A", "A", "severity", null, null, false, null, [], []), new CriticalTableCellDetail(12, "06-10", "B", "B", "severity", "A", "Alpha", false, null, [], []));

        var byResult = TableContextCellResolver.FindCell(detail, new TableContextSnapshot(ResultId: 12));
        var byLocation = TableContextCellResolver.FindCell(detail, new TableContextSnapshot(GroupKey: "A", ColumnKey: "B", RollBand: "06-10"));

        Assert.Equal(12, byResult!.ResultId);
        Assert.Equal(12, byLocation!.ResultId);
    }

    [Fact]
    public void Find_cell_returns_null_when_context_does_not_identify_a_cell()
    {
        var detail = CreateDetail(new CriticalTableCellDetail(12, "06-10", "B", "B", "severity", "A", "Alpha", false, null, [], []));

        Assert.Null(TableContextCellResolver.FindCell(detail, new TableContextSnapshot()));
        Assert.Null(TableContextCellResolver.FindCell(detail, new TableContextSnapshot(GroupKey: "B", ColumnKey: "B", RollBand: "06-10")));
    }

    private static CriticalTableDetail CreateDetail(params CriticalTableCellDetail[] cells) =>
        new("slash", "Slash Critical Strike Table", "standard", "Slash.pdf", null, [
            new CriticalColumnReference("A", "A", "severity", 1),
            new CriticalColumnReference("B", "B", "severity", 2),
            new CriticalColumnReference("C", "C", "severity", 3)
        ], [
            new CriticalGroupReference("A", "Alpha", 1),
            new CriticalGroupReference("B", "Beta", 2)
        ], [
            new CriticalRollBandReference("01-05", 1, 5, 1),
            new CriticalRollBandReference("06-10", 6, 10, 2),
            new CriticalRollBandReference("21-25", 21, 25, 3)
        ], cells, []);
}