using System.Collections.Generic;
using System.Linq;
using CncModels.Dto;
using CncModels.Dto.Common;
using CncWebApi.Controllers;
using Xunit;

namespace CncWebApi.Tests
{
    /// <summary>
    /// OptionController单元测试
    /// 公共下拉选项接口（车间/品牌/机床/工人/采集地址）
    /// </summary>
    [Collection("Database")]
    public class OptionControllerTests
    {
        private readonly OptionController _controller;

        public OptionControllerTests()
        {
            TestDb.TruncateAll();
            _controller = ControllerFactory.CreateOptionController();
        }

        #region WorkshopList - 车间下拉

        /// <summary>
        /// 测试：车间下拉返回种子数据
        /// </summary>
        [Fact]
        public void WorkshopList_ShouldReturnSeedData()
        {
            var result = _controller.WorkshopList();
            var response = ControllerFactory.Extract<List<SimpleOption>>(result);
            ControllerFactory.AssertSuccess(response);
            Assert.Equal(2, response.Data.Count); // A栋、B栋
        }

        #endregion

        #region BrandList - 品牌下拉

        /// <summary>
        /// 测试：品牌下拉返回种子数据
        /// </summary>
        [Fact]
        public void BrandList_ShouldReturnSeedData()
        {
            var result = _controller.BrandList();
            var response = ControllerFactory.Extract<List<SimpleOption>>(result);
            ControllerFactory.AssertSuccess(response);
            Assert.Single(response.Data); // FANUC
            Assert.Equal("FANUC", response.Data[0].Label);
        }

        #endregion

        #region MachineList - 机床下拉

        /// <summary>
        /// 测试：空数据库返回空列表
        /// </summary>
        [Fact]
        public void MachineList_EmptyDb_ShouldReturnEmpty()
        {
            var result = _controller.MachineList();
            var response = ControllerFactory.Extract<List<SimpleOption>>(result);
            ControllerFactory.AssertSuccess(response);
            Assert.Empty(response.Data);
        }

        /// <summary>
        /// 测试：有机床时返回选项
        /// </summary>
        [Fact]
        public void MachineList_WithData_ShouldReturnOptions()
        {
            TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
                VALUES ('测试地址', 'http://192.168.1.1', 1, 5, 1, NOW(), NOW())");
            TestDb.Execute(@"INSERT INTO cnc_machine (device_code, name, workshop_id, collect_address_id, ip_address, brand_id, is_enabled, created_at, updated_at)
                VALUES ('CNC001', '机床1', 1, 1, '192.168.1.100', 1, 1, NOW(), NOW())");

            // 重新创建Controller以获取最新数据
            var controller = ControllerFactory.CreateOptionController();
            var result = controller.MachineList();
            var response = ControllerFactory.Extract<List<SimpleOption>>(result);
            Assert.Single(response.Data);
        }

        #endregion

        #region WorkerList - 工人下拉

        /// <summary>
        /// 测试：空数据库返回空列表
        /// </summary>
        [Fact]
        public void WorkerList_EmptyDb_ShouldReturnEmpty()
        {
            var result = _controller.WorkerList();
            var response = ControllerFactory.Extract<List<SimpleOption>>(result);
            ControllerFactory.AssertSuccess(response);
            Assert.Empty(response.Data);
        }

        /// <summary>
        /// 测试：有工人时返回选项（格式：姓名(工号)）
        /// </summary>
        [Fact]
        public void WorkerList_WithData_ShouldReturnFormattedOptions()
        {
            TestDb.Execute(@"INSERT INTO cnc_worker (name, code, is_enabled, created_at, updated_at)
                VALUES ('张三', 'W001', 1, NOW(), NOW())");

            var controller = ControllerFactory.CreateOptionController();
            var result = controller.WorkerList();
            var response = ControllerFactory.Extract<List<SimpleOption>>(result);
            Assert.Single(response.Data);
            Assert.Equal("张三(W001)", response.Data[0].Label);
        }

        #endregion

        #region CollectAddressList - 采集地址下拉

        /// <summary>
        /// 测试：空数据库返回空列表
        /// </summary>
        [Fact]
        public void CollectAddressList_EmptyDb_ShouldReturnEmpty()
        {
            var result = _controller.CollectAddressList();
            var response = ControllerFactory.Extract<List<SimpleOption>>(result);
            ControllerFactory.AssertSuccess(response);
            Assert.Empty(response.Data);
        }

        /// <summary>
        /// 测试：有地址时返回选项
        /// </summary>
        [Fact]
        public void CollectAddressList_WithData_ShouldReturnOptions()
        {
            TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
                VALUES ('测试地址', 'http://192.168.1.1', 1, 5, 1, NOW(), NOW())");

            var controller = ControllerFactory.CreateOptionController();
            var result = controller.CollectAddressList();
            var response = ControllerFactory.Extract<List<SimpleOption>>(result);
            Assert.Single(response.Data);
            Assert.Equal("测试地址", response.Data[0].Label);
        }

        #endregion
    }
}
