using Theriapolis.Core.World;
using Theriapolis.Core.World.Settlements;
using Xunit;

namespace Theriapolis.Tests.Settlements;

/// <summary>
/// Phase 6 M1 — AnchorRegistry semantics.
/// </summary>
public sealed class AnchorRegistryTests
{
    [Fact]
    public void Anchor_RegistersAndResolves()
    {
        var r = new AnchorRegistry();
        r.RegisterAnchor(NarrativeAnchor.Millhaven, settlementId: 42);
        Assert.Equal(42, r.ResolveAnchor("anchor:millhaven"));
    }

    [Fact]
    public void Anchor_LookupIsCaseInsensitive()
    {
        var r = new AnchorRegistry();
        r.RegisterAnchor(NarrativeAnchor.Millhaven, settlementId: 42);
        Assert.Equal(42, r.ResolveAnchor("ANCHOR:MILLHAVEN"));
        Assert.Equal(42, r.ResolveAnchor("anchor:Millhaven"));
    }

    [Fact]
    public void UnregisteredAnchor_ReturnsNull()
    {
        var r = new AnchorRegistry();
        Assert.Null(r.ResolveAnchor("anchor:doesnotexist"));
    }

    [Fact]
    public void NamedRole_RegistersAndResolves()
    {
        var r = new AnchorRegistry();
        r.RegisterRole("millhaven.innkeeper", npcId: 777);
        Assert.Equal(777, r.ResolveRole("role:millhaven.innkeeper"));
    }

    [Fact]
    public void GenericRoleTag_DoesNotRegister()
    {
        // A bare role tag without a "settlement.role" qualifier shouldn't
        // be globally addressable — there are many generic innkeepers.
        var r = new AnchorRegistry();
        r.RegisterRole("innkeeper", npcId: 5);
        Assert.Null(r.ResolveRole("role:innkeeper"));
    }

    [Fact]
    public void UnregisterRole_RemovesEntry()
    {
        var r = new AnchorRegistry();
        r.RegisterRole("millhaven.innkeeper", npcId: 5);
        r.UnregisterRole("millhaven.innkeeper");
        Assert.Null(r.ResolveRole("role:millhaven.innkeeper"));
    }

    [Fact]
    public void Clear_DropsAllEntries()
    {
        var r = new AnchorRegistry();
        r.RegisterAnchor(NarrativeAnchor.Millhaven, 1);
        r.RegisterRole("millhaven.innkeeper", 5);
        r.Clear();
        Assert.Null(r.ResolveAnchor("anchor:millhaven"));
        Assert.Null(r.ResolveRole("role:millhaven.innkeeper"));
        Assert.Empty(r.AllAnchors);
        Assert.Empty(r.AllRoles);
    }
}
