using System;
using CncModels.Constants;
using CncModels.Entity;
using CncService;
using CncService.Impl;
using Xunit;

namespace CncService.Tests
{
    /// <summary>
    /// ScreenService 大屏配置测试
    /// 测试场景：获取/更新配置、筛选CRUD、汇总数据、参数校验
    /// </summary>
    [Collection("Database")]
    public class ScreenServiceTests : IDisposable
    {
        private readonly ScreenService _service;

        public ScreenServiceTests()
        {
            TestDb.TruncateAll();
            _service = ServiceFactory.CreateScreenService();
        }

        public void Dispose()
        {
            TestDb.TruncateAll();
        }

        // ======== GetSummary ========

        [Fact]
        public void GetSummary_返回默认汇总()
        {
            var summary = _service.GetSummary();
            Assert.NotNull(summary);
            Assert.Equal(0, summary.MachineCount);
            Assert.Equal(0, summary.ProductionToday);
            Assert.Equal(0, summary.AlertCount);
            Assert.Equal(0, summary.OnlineCount);
        }

        // ======== GetConfigs ========

        [Fact]
        public void GetConfigs_无数据_返回空列表()
        {
            var configs = _service.GetConfigs();
            Assert.NotNull(configs);
        }

        // ======== UpdateConfig ========

        [Fact]
        public void UpdateConfig_请求为null_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.UpdateConfig(null));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        // ======== GetFilters ========

        [Fact]
        public void GetFilters_screenKey为空_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.GetFilters(""));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void GetFilters_screenKey为null_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.GetFilters(null));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void GetFilters_screenKey为空格_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.GetFilters("   "));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void GetFilters_存在的screenKey_返回列表()
        {
            // 先插入筛选数据
            TestDb.Execute(@"INSERT INTO cnc_screen_filter (screen_key, filter_type, filter_value, is_default, sort_order)
                VALUES ('dashboard', 'workshop', '1', 1, 1)");

            var filters = _service.GetFilters("dashboard");
            Assert.NotNull(filters);
            Assert.True(filters.Count >= 1);
        }

        // ======== CreateFilter ========

        [Fact]
        public void CreateFilter_正常创建_返回自增ID()
        {
            var id = _service.CreateFilter(new ScreenFilter
            {
                ScreenKey = "dashboard",
                FilterType = "workshop",
                FilterValue = "1",
                IsDefault = 1,
                SortOrder = 1
            });
            Assert.True(id > 0);
        }

        [Fact]
        public void CreateFilter_请求为null_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.CreateFilter(null));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        // ======== UpdateFilter ========

        [Fact]
        public void UpdateFilter_请求为null_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.UpdateFilter(null));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        // ======== DeleteFilter ========

        [Fact]
        public void DeleteFilter_无效ID_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.DeleteFilter(0));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void DeleteFilter_负数ID_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.DeleteFilter(-1));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void DeleteFilter_存在的ID_返回true()
        {
            var id = _service.CreateFilter(new ScreenFilter
            {
                ScreenKey = "dashboard",
                FilterType = "workshop",
                FilterValue = "1",
                IsDefault = 1,
                SortOrder = 1
            });

            var result = _service.DeleteFilter(id);
            Assert.True(result);
        }
    }
}
