using ErsatzTV.Core.Domain;
using ErsatzTV.Core.FFmpeg;
using NUnit.Framework;
using Shouldly;

namespace ErsatzTV.Core.Tests.FFmpeg;

/// <summary>
///     Pins which watermarks ffmpeg may carry natively and which must go to the graphics engine.
///     The remote-URL rule is the second half of the #502 fix: resolving the URL is useless if the
///     resolved path is then handed to ffmpeg as a bare <c>-i</c> argument.
/// </summary>
[TestFixture]
public class FFmpegNativeWatermarkRoutingTests
{
    private const string LocalPath = "/cache/logos/ab/abc123.png";

    private static WatermarkOptions Options(
        string imagePath,
        ChannelWatermarkMode mode = ChannelWatermarkMode.Permanent) =>
        new(new ChannelWatermark { Id = 1, Name = "wm", Mode = mode }, imagePath, Option<int>.None);

    [Test]
    public void Local_Path_Single_Permanent_Watermark_Uses_FFmpeg()
    {
        FFmpegLibraryProcessService.CanUseFFmpegNativeWatermark(0, [Options(LocalPath)])
            .ShouldBeTrue();
    }

    [TestCase("https://cdn.example.com/logos/channel.png")]
    [TestCase("http://cdn.example.com/logos/channel.png")]
    public void Remote_Url_Watermark_Goes_To_Graphics_Engine(string url)
    {
        FFmpegLibraryProcessService.CanUseFFmpegNativeWatermark(0, [Options(url)])
            .ShouldBeFalse();
    }

    /// <summary>
    ///     The generated-initials fallback is a localhost URL. Only the deco path still emits it, and it is
    ///     routed by its resolved path like any other URL — see the #502 entry in docs/decisions.md and #510.
    /// </summary>
    [Test]
    public void Generated_Localhost_Logo_Url_Goes_To_Graphics_Engine()
    {
        FFmpegLibraryProcessService
            .CanUseFFmpegNativeWatermark(0, [Options("http://localhost:8409/iptv/logos/gen?text=Test")])
            .ShouldBeFalse();
    }

    [Test]
    public void Graphics_Elements_Present_Goes_To_Graphics_Engine()
    {
        FFmpegLibraryProcessService.CanUseFFmpegNativeWatermark(1, [Options(LocalPath)])
            .ShouldBeFalse();
    }

    [Test]
    public void Multiple_Watermarks_Go_To_Graphics_Engine()
    {
        FFmpegLibraryProcessService.CanUseFFmpegNativeWatermark(0, [Options(LocalPath), Options(LocalPath)])
            .ShouldBeFalse();
    }

    [Test]
    public void No_Watermarks_Does_Not_Use_FFmpeg()
    {
        FFmpegLibraryProcessService.CanUseFFmpegNativeWatermark(0, []).ShouldBeFalse();
    }

    [TestCase(ChannelWatermarkMode.Intermittent)]
    [TestCase(ChannelWatermarkMode.None)]
    public void Non_Permanent_Watermark_Goes_To_Graphics_Engine(ChannelWatermarkMode mode)
    {
        FFmpegLibraryProcessService.CanUseFFmpegNativeWatermark(0, [Options(LocalPath, mode)])
            .ShouldBeFalse();
    }
}
