using Microsoft.VisualStudio.TestTools.UnitTesting;
using YMhut.Box.Core.Tools;

namespace YMhut.Box.Tests;

[TestClass]
public sealed class ToolResultExperienceCatalogTests
{
    [TestMethod]
    public void EveryDefaultToolHasUniqueResultExperience()
    {
        var catalog = new ToolCatalog(ToolCatalog.DefaultModules());
        var experiences = new ToolResultExperienceCatalog(catalog);

        Assert.AreEqual(catalog.Modules.Count, experiences.Experiences.Count);

        var ids = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
        foreach (var module in catalog.Modules)
        {
            var experience = experiences.GetRequired(module.Id);
            Assert.AreEqual(module.Id, experience.ToolId);
            Assert.IsFalse(string.IsNullOrWhiteSpace(experience.ExperienceId), module.Id);
            Assert.IsFalse(string.IsNullOrWhiteSpace(experience.Layout), module.Id);
            Assert.IsFalse(string.IsNullOrWhiteSpace(experience.PrimaryPrimitive), module.Id);
            Assert.IsTrue(ids.Add(experience.ExperienceId), $"Duplicate experienceId: {experience.ExperienceId}");
        }
    }

    [TestMethod]
    public void EveryDefaultToolHasUniquePageExperience()
    {
        var catalog = new ToolCatalog(ToolCatalog.DefaultModules());
        var experiences = new ToolResultExperienceCatalog(catalog);

        Assert.AreEqual(catalog.Modules.Count, experiences.PageExperiences.Count);

        var ids = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
        foreach (var module in catalog.Modules)
        {
            var experience = experiences.GetRequiredPage(module.Id);
            Assert.AreEqual(module.Id, experience.ToolId);
            Assert.IsFalse(string.IsNullOrWhiteSpace(experience.ExperienceId), module.Id);
            Assert.IsFalse(string.IsNullOrWhiteSpace(experience.InputLayout), module.Id);
            Assert.IsFalse(string.IsNullOrWhiteSpace(experience.ResultLayout), module.Id);
            Assert.IsTrue(experience.InputPrimitives.Count > 0, module.Id);
            Assert.IsTrue(experience.ResultPrimitives.Count > 0, module.Id);
            Assert.IsTrue(ids.Add(experience.ExperienceId), $"Duplicate page experienceId: {experience.ExperienceId}");
        }
    }

    [TestMethod]
    public void PrivacySanitizerOnlyRedactsYmHutAndApiRequestUrls()
    {
        const string input =
            "public https://example.com/path?q=1; ymhut https://update.ymhut.cn/update-info.json; api_url=https://api.example.net/v1/items";

        var sanitized = ToolResultPrivacySanitizer.Redact(input, "en-US");

        StringAssert.Contains(sanitized, "https://example.com/path?q=1");
        Assert.IsFalse(sanitized.Contains("update.ymhut.cn", StringComparison.OrdinalIgnoreCase), sanitized);
        Assert.IsFalse(sanitized.Contains("api.example.net", StringComparison.OrdinalIgnoreCase), sanitized);
        StringAssert.Contains(sanitized, "[YMhut endpoint hidden]");
    }
}
