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

namespace YMhut.Box.Tests;

[TestClass]
public sealed class ToolRemoteRankingTests
{
    [TestMethod]
    public void HotboardUsesPlatformSelectionBeforeRunning()
    {
        var module = new ToolCatalog().GetById("hotboard")!;
        var spec = ToolPageSpecCatalog.For(module);

        Assert.AreEqual(ToolPrimaryInputKind.FixedOptions, spec.PrimaryInput);
        Assert.IsFalse(spec.AutoRunOnOpen);
        Assert.IsTrue(spec.Parameters.Any(parameter =>
            parameter.Key == "preset" &&
            parameter.Options.Any(option => option.Value == "百度")));
    }

    [TestMethod]
    public void HotboardPlatformResolvesDailyHotTitleParameter()
    {
        var ok = ApiEndpoints.TryResolve("hotboard", "百度", out var endpoint, out var uri);

        Assert.IsTrue(ok);
        Assert.AreEqual("hotboard", endpoint.Id);
        StringAssert.Contains(Uri.UnescapeDataString(uri.Query), "title=百度");
    }

    [TestMethod]
    public void DailyHotRankingKeepsPublicUriOutOfVisibleTitle()
    {
        Assert.IsTrue(ApiEndpoints.TryGet("hotboard", out var endpoint));
        var uri = new Uri("https://example.invalid/api/dailyhot?title=test");
        var response = new ApiResponse(
            endpoint.Id,
            uri,
            Success: true,
            Content: string.Empty,
            Error: null,
            FetchedAt: DateTimeOffset.UnixEpoch);
        var document = ToolResultBuilder.FromRemote(
            endpoint,
            response,
            """
            1. YMhut 发布新版本 / 热度 999 / https://example.com/news?id=1
            2. 第二条 https://example.com/news?id=2
            """);

        var ranked = document.Blocks.First(block => block.Kind == ToolResultBlockKind.RankedList);
        var first = ranked.Items[0];

        Assert.AreEqual("https://example.com/news?id=1", first.Uri);
        Assert.IsFalse(first.Title.Contains("https://", StringComparison.OrdinalIgnoreCase));
        Assert.IsTrue(ranked.Items.All(item => !item.Title.Contains("https://", StringComparison.OrdinalIgnoreCase)));
    }
}
