using Xunit;
using XplorePlane.ViewModels;
using XplorePlane.Tests.Helpers;
using System;

namespace XplorePlane.Tests.Pipeline
{
    /// <summary>
    /// 测试 OperatorToolboxViewModel 搜索过滤逻辑（任务 6.7）
    /// </summary>
    public class OperatorToolboxViewModelTests
    {
        private OperatorToolboxViewModel CreateVm(string[]? keys = null)
        {
            var mock = TestHelpers.CreateMockImageService(keys);
            return new OperatorToolboxViewModel(mock.Object);
        }

        [Fact]
        public void EmptySearch_ReturnsAllOperators()
        {
            var vm = CreateVm(new[] { "Blur", "Sharpen", "Threshold" });
            vm.SearchText = string.Empty;

            Assert.Equal(3, vm.FilteredOperators.Count);
        }

        [Fact]
        public void NullSearch_ReturnsAllOperators()
        {
            var vm = CreateVm(new[] { "Blur", "Sharpen", "Threshold" });
            vm.SearchText = null!;

            Assert.Equal(3, vm.FilteredOperators.Count);
        }

        [Fact]
        public void WhitespaceSearch_ReturnsAllOperators()
        {
            var vm = CreateVm(new[] { "Blur", "Sharpen", "Threshold" });
            vm.SearchText = "   ";

            Assert.Equal(3, vm.FilteredOperators.Count);
        }

        [Fact]
        public void SearchText_FiltersMatchingOperators()
        {
            // DisplayName = Key + "算子"，所以 "Blur算子" 包含 "Blur"
            var vm = CreateVm(new[] { "Blur", "Sharpen", "Threshold" });
            vm.SearchText = "Blur";

            Assert.Single(vm.FilteredOperators);
            Assert.Equal("Blur", vm.FilteredOperators[0].Key);
        }

        [Fact]
        public void SearchText_CaseInsensitive()
        {
            var vm = CreateVm(new[] { "Blur", "Sharpen", "Threshold" });
            vm.SearchText = "blur";

            Assert.Single(vm.FilteredOperators);
        }

        [Fact]
        public void SearchText_NoMatch_ReturnsEmpty()
        {
            var vm = CreateVm(new[] { "Blur", "Sharpen", "Threshold" });
            vm.SearchText = "XYZ_NOT_EXIST";

            Assert.Empty(vm.FilteredOperators);
        }

        [Fact]
        public void SearchText_PartialMatch_ReturnsMatching()
        {
            var vm = CreateVm(new[] { "Blur", "Sharpen", "Threshold" });
            vm.SearchText = "算子"; // 所有 DisplayName 都包含"算子"

            Assert.Equal(3, vm.FilteredOperators.Count);
        }

        [Fact]
        public void FilteredOperators_EachContainsSearchText()
        {
            var vm = CreateVm(new[] { "Blur", "Sharpen", "Threshold" });
            vm.SearchText = "Sh";

            foreach (var op in vm.FilteredOperators)
                Assert.Contains("Sh", op.DisplayName, StringComparison.OrdinalIgnoreCase);
        }

        [Fact]
        public void NoOperators_EmptySearch_ReturnsEmpty()
        {
            var vm = CreateVm(Array.Empty<string>());
            vm.SearchText = string.Empty;

            Assert.Empty(vm.FilteredOperators);
        }
    }
}
