using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms;

namespace ZB.MOM.WW.OtOpcUa.Core.ScriptedAlarms.Tests;

[Trait("Category", "Unit")]
public sealed class MessageTemplateTests
{
    private static DataValueSnapshot Good(object? v) =>
        new(v, 0u, DateTime.UtcNow, DateTime.UtcNow);
    private static DataValueSnapshot Bad() =>
        new(null, 0x80050000u, null, DateTime.UtcNow);

    private static DataValueSnapshot? Resolver(Dictionary<string, DataValueSnapshot> map, string path)
        => map.TryGetValue(path, out var v) ? v : null;

    /// <summary>Verifies template with no tokens is returned unchanged.</summary>
    [Fact]
    public void No_tokens_returns_template_unchanged()
    {
        MessageTemplate.Resolve("No tokens here", _ => null).ShouldBe("No tokens here");
    }

    /// <summary>Verifies single token in template is correctly substituted.</summary>
    [Fact]
    public void Single_token_substituted()
    {
        var map = new Dictionary<string, DataValueSnapshot> { ["Tank/Temp"] = Good(75.5) };
        MessageTemplate.Resolve("Temp={Tank/Temp}C", p => Resolver(map, p)).ShouldBe("Temp=75.5C");
    }

    /// <summary>Verifies multiple tokens in template are all substituted.</summary>
    [Fact]
    public void Multiple_tokens_substituted()
    {
        var map = new Dictionary<string, DataValueSnapshot>
        {
            ["A"] = Good(10),
            ["B"] = Good("on"),
        };
        MessageTemplate.Resolve("{A}/{B}", p => Resolver(map, p)).ShouldBe("10/on");
    }

    /// <summary>Verifies tokens with bad quality become question marks.</summary>
    [Fact]
    public void Bad_quality_token_becomes_question_mark()
    {
        var map = new Dictionary<string, DataValueSnapshot> { ["Bad"] = Bad() };
        MessageTemplate.Resolve("value={Bad}", p => Resolver(map, p)).ShouldBe("value={?}");
    }

    /// <summary>Verifies unknown token paths become question marks.</summary>
    [Fact]
    public void Unknown_path_becomes_question_mark()
    {
        MessageTemplate.Resolve("value={DoesNotExist}", _ => null).ShouldBe("value={?}");
    }

    /// <summary>Verifies null values with good quality become question marks.</summary>
    [Fact]
    public void Null_value_with_good_quality_becomes_question_mark()
    {
        var map = new Dictionary<string, DataValueSnapshot> { ["X"] = Good(null) };
        MessageTemplate.Resolve("{X}", p => Resolver(map, p)).ShouldBe("{?}");
    }

    /// <summary>Verifies tokens containing slashes and dots are correctly resolved.</summary>
    [Fact]
    public void Tokens_with_slashes_and_dots_resolved()
    {
        var map = new Dictionary<string, DataValueSnapshot>
        {
            ["Line1/Pump.Speed"] = Good(1200),
        };
        MessageTemplate.Resolve("rpm={Line1/Pump.Speed}", p => Resolver(map, p))
            .ShouldBe("rpm=1200");
    }

    /// <summary>Verifies empty template returns an empty string.</summary>
    [Fact]
    public void Empty_template_returns_empty()
    {
        MessageTemplate.Resolve("", _ => null).ShouldBe("");
    }

    /// <summary>Verifies null template returns an empty string without throwing.</summary>
    [Fact]
    public void Null_template_returns_empty_without_throwing()
    {
        MessageTemplate.Resolve(null!, _ => null).ShouldBe("");
    }

    /// <summary>Verifies ExtractTokenPaths returns all token paths from a template.</summary>
    [Fact]
    public void ExtractTokenPaths_returns_every_distinct_token()
    {
        var tokens = MessageTemplate.ExtractTokenPaths("{A}/{B}/{A}/{C}");
        tokens.ShouldBe(new[] { "A", "B", "A", "C" });
    }

    /// <summary>Verifies ExtractTokenPaths returns empty for templates without tokens.</summary>
    [Fact]
    public void ExtractTokenPaths_empty_for_tokenless_template()
    {
        MessageTemplate.ExtractTokenPaths("No tokens").ShouldBeEmpty();
        MessageTemplate.ExtractTokenPaths("").ShouldBeEmpty();
        MessageTemplate.ExtractTokenPaths(null).ShouldBeEmpty();
    }

    /// <summary>Verifies whitespace inside token is trimmed during resolution.</summary>
    [Fact]
    public void Whitespace_inside_token_is_trimmed()
    {
        var map = new Dictionary<string, DataValueSnapshot> { ["A"] = Good(42) };
        MessageTemplate.Resolve("{  A  }", p => Resolver(map, p)).ShouldBe("42");
    }
}
