namespace RpgRoller.Tests;

public sealed class ServiceD6RollTests
{
    [Fact]
    public void RollSkill_D6WildCritical_AddsExtraDieAndTracksFlags()
    {
        using var harness = ServiceTestSupport.CreateHarness(6, 4, 2);
        var service = harness.Service;

        service.Register("gm", "Password123", "GM");
        var session = ServiceTestSupport.GetValue(service.Login("gm", "Password123")).SessionToken;
        var campaign = ServiceTestSupport.GetValue(service.CreateCampaign(session, "Main", "d6"));
        var character = ServiceTestSupport.GetValue(service.CreateCharacter(session, "Hero", campaign.Id));
        var skill = ServiceTestSupport.GetValue(service.CreateSkill(session, character.Id, "Blaster", "2D+1", 1, true));

        var roll = ServiceTestSupport.GetValue(service.RollSkill(session, skill.Id, "public"));

        Assert.Equal(13, roll.Result);
        Assert.Equal("6+4+2+1=13", roll.Breakdown);
        Assert.Equal(3, roll.Dice.Count);
        Assert.True(roll.Dice[0].Wild);
        Assert.True(roll.Dice[0].Crit);
        Assert.True(roll.Dice[2].Added);
    }

    [Fact]
    public void RollSkill_D6Fumble_RemovesHighestDieAndPreservesFumbleDie()
    {
        using var harness = ServiceTestSupport.CreateHarness(1, 3, 6, 1);
        var service = harness.Service;

        service.Register("gm", "Password123", "GM");
        var session = ServiceTestSupport.GetValue(service.Login("gm", "Password123")).SessionToken;
        var campaign = ServiceTestSupport.GetValue(service.CreateCampaign(session, "Main", "d6"));
        var character = ServiceTestSupport.GetValue(service.CreateCharacter(session, "Hero", campaign.Id));
        var skill = ServiceTestSupport.GetValue(service.CreateSkill(session, character.Id, "Brawl", "3D", 1, true));

        var roll = ServiceTestSupport.GetValue(service.RollSkill(session, skill.Id, "public"));

        Assert.Equal(4, roll.Result);
        Assert.Equal("1+3=4", roll.Breakdown);
        Assert.Equal(3, roll.Dice.Count);
        Assert.True(roll.Dice[0].Fumble);
        Assert.True(roll.Dice[0].Wild);
        Assert.True(roll.Dice[2].Removed);
        Assert.False(roll.Dice[2].Crit);
        Assert.False(roll.Dice[2].Fumble);

        var noFumbleSkill = ServiceTestSupport.GetValue(service.CreateSkill(session, character.Id, "Calm", "1D", 1, false));
        var noFumbleRoll = ServiceTestSupport.GetValue(service.RollSkill(session, noFumbleSkill.Id, "public"));
        Assert.False(noFumbleRoll.Dice[0].Fumble);
    }

    [Fact]
    public void SkillOptions_AreValidatedForD6AndIgnoredForDnd5e()
    {
        using var harness = ServiceTestSupport.CreateHarness(2, 2);
        var service = harness.Service;

        service.Register("gm", "Password123", "GM");
        var session = ServiceTestSupport.GetValue(service.Login("gm", "Password123")).SessionToken;

        var d6Campaign = ServiceTestSupport.GetValue(service.CreateCampaign(session, "D6", "d6"));
        var d6Character = ServiceTestSupport.GetValue(service.CreateCharacter(session, "D6 Hero", d6Campaign.Id));

        var missingWildDice = service.CreateSkill(session, d6Character.Id, "Broken", "2D+1", 0, true);
        Assert.False(missingWildDice.Succeeded);

        var tooManyWildDice = service.CreateSkill(session, d6Character.Id, "Broken 2", "2D+1", 51, true);
        Assert.False(tooManyWildDice.Succeeded);

        var dndCampaign = ServiceTestSupport.GetValue(service.CreateCampaign(session, "DND", "dnd5e"));
        var dndCharacter = ServiceTestSupport.GetValue(service.CreateCharacter(session, "Mage", dndCampaign.Id));
        var dndSkill = ServiceTestSupport.GetValue(service.CreateSkill(session, dndCharacter.Id, "Arcana", "1d20+2", 5, true));

        Assert.Equal(0, dndSkill.WildDice);
        Assert.False(dndSkill.AllowFumble);
    }
}