using Theriapolis.Core.Data;
using Theriapolis.Core.Entities;
using Theriapolis.Core.Rules.Character;
using Theriapolis.Core.Rules.Stats;
using Theriapolis.Core.Util;
using Xunit;

namespace Theriapolis.Tests.Entities;

public sealed class ActorCharacterTests
{
    private readonly ContentResolver _content = new(new ContentLoader(TestHelpers.DataDirectory));

    [Fact]
    public void SpawnPlayer_WithCharacter_AttachesIt()
    {
        var mgr = new ActorManager();
        var character = MakeCharacter();
        var p = mgr.SpawnPlayer(new Vec2(100, 200), character);
        Assert.NotNull(p.Character);
        Assert.Equal("Wolf-Folk", p.Character!.Species.Name);
        Assert.Equal(Allegiance.Player, p.Allegiance);
    }

    [Fact]
    public void SpawnPlayer_NoCharacter_LeavesItNull()
    {
        var mgr = new ActorManager();
        var p = mgr.SpawnPlayer(new Vec2(100, 200));
        Assert.Null(p.Character);
        Assert.True(p.IsAlive);  // null-character actors are considered alive
    }

    [Fact]
    public void Actor_IsAlive_ReflectsCharacterHp()
    {
        var mgr = new ActorManager();
        var character = MakeCharacter();
        var p = mgr.SpawnPlayer(new Vec2(0, 0), character);
        Assert.True(p.IsAlive);
        character.CurrentHp = 0;
        Assert.False(p.IsAlive);
        character.Conditions.Add(Condition.Unconscious);
        Assert.True(p.IsAlive);  // unconscious counts as alive (death-save loop)
    }

    private Character MakeCharacter()
    {
        return new CharacterBuilder
        {
            Clade        = _content.Clades["canidae"],
            Species      = _content.Species["wolf"],
            ClassDef     = _content.Classes["fangsworn"],
            Background   = _content.Backgrounds["pack_raised"],
            BaseAbilities = new AbilityScores(15, 14, 13, 12, 10, 8),
            Name         = "Test",
        }
        .ChooseSkill(SkillId.Athletics)
        .ChooseSkill(SkillId.Intimidation)
        .Build();
    }
}
