namespace RpgRoller.Tests;

public sealed class DiceRulesTests
{
    [Fact]
    public void DiceRules_CoversParsingAndMappingBranches()
    {
        Assert.Equal(RulesetKind.D6, DiceRules.TryParseRulesetId("d6"));
        Assert.Equal(RulesetKind.Dnd5e, DiceRules.TryParseRulesetId("dnd5e"));
        Assert.Equal(RulesetKind.Rolemaster, DiceRules.TryParseRulesetId("rolemaster"));
        Assert.Null(DiceRules.TryParseRulesetId("unknown"));

        var d6 = DiceRules.ParseExpression(RulesetKind.D6, "5D+4");
        var dnd = DiceRules.ParseExpression(RulesetKind.Dnd5e, "2d12+2");
        var rolemasterImplicitSingle = DiceRules.ParseExpression(RulesetKind.Rolemaster, "d10");
        var rolemasterManyDice = DiceRules.ParseExpression(RulesetKind.Rolemaster, "15d10-15");
        var rolemasterPercentile = DiceRules.ParseExpression(RulesetKind.Rolemaster, "d100+4");
        var rolemasterOpenEnded = DiceRules.ParseExpression(RulesetKind.Rolemaster, "1d100!+85");
        var emptyExpression = DiceRules.ParseExpression(RulesetKind.Dnd5e, "");
        var badFormat = DiceRules.ParseExpression(RulesetKind.Dnd5e, "abc");
        var tooManyDice = DiceRules.ParseExpression(RulesetKind.D6, "51D+1");
        var tooManySides = DiceRules.ParseExpression(RulesetKind.Dnd5e, "1d1001");
        var tooLargeModifier = DiceRules.ParseExpression(RulesetKind.Dnd5e, "1d20+1001");
        var negativeDndModifier = DiceRules.ParseExpression(RulesetKind.Dnd5e, "1d20-1");
        var invalidRolemasterOpenEndedFormat = DiceRules.ParseExpression(RulesetKind.Rolemaster, "2d10!+1");
        var tooNegativeRolemasterModifier = DiceRules.ParseExpression(RulesetKind.Rolemaster, "d100-1001");
        var unknownRulesetExpression = DiceRules.ParseExpression((RulesetKind)99, "1d20+1");

        Assert.True(d6.Succeeded);
        Assert.True(dnd.Succeeded);
        Assert.True(rolemasterImplicitSingle.Succeeded);
        Assert.True(rolemasterManyDice.Succeeded);
        Assert.True(rolemasterPercentile.Succeeded);
        Assert.True(rolemasterOpenEnded.Succeeded);
        Assert.False(emptyExpression.Succeeded);
        Assert.False(badFormat.Succeeded);
        Assert.False(tooManyDice.Succeeded);
        Assert.False(tooManySides.Succeeded);
        Assert.False(tooLargeModifier.Succeeded);
        Assert.False(negativeDndModifier.Succeeded);
        Assert.False(invalidRolemasterOpenEndedFormat.Succeeded);
        Assert.False(tooNegativeRolemasterModifier.Succeeded);
        Assert.False(unknownRulesetExpression.Succeeded);

        Assert.Equal("d10", rolemasterImplicitSingle.Value!.Canonical);
        Assert.Equal(DiceExpressionKind.Standard, rolemasterImplicitSingle.Value.Kind);
        Assert.Equal("15d10-15", rolemasterManyDice.Value!.Canonical);
        Assert.Equal(DiceExpressionKind.Standard, rolemasterManyDice.Value.Kind);
        Assert.Equal("d100+4", rolemasterPercentile.Value!.Canonical);
        Assert.Equal(DiceExpressionKind.Standard, rolemasterPercentile.Value.Kind);
        Assert.Equal("d100!+85", rolemasterOpenEnded.Value!.Canonical);
        Assert.Equal(DiceExpressionKind.RolemasterOpenEndedPercentile, rolemasterOpenEnded.Value.Kind);

        Assert.Equal("d6", DiceRules.ToRulesetId(RulesetKind.D6));
        Assert.Equal("dnd5e", DiceRules.ToRulesetId(RulesetKind.Dnd5e));
        Assert.Equal("rolemaster", DiceRules.ToRulesetId(RulesetKind.Rolemaster));
        Assert.Throws<ArgumentOutOfRangeException>(() => DiceRules.ToRulesetId((RulesetKind)99));
    }
}