using System;
using System.Linq;
using CncModels.Dto;
using CncModels.Dto.Machine;
using CncModels.Entity;
using CncRepository.Impl;
using Xunit;

namespace CncRepository.Tests
{
    /// <summary>
    /// 机床仓储测试
    /// </summary>
    [Collection("Database")]
    public class MachineRepositoryTests : IDisposable
    {
        private readonly MachineRepository _repo;

        public MachineRepositoryTests()
        {
            _repo = new MachineRepository(TestDb.ConnectionString);
            TestDb.TruncateAll();
            // 插入一个采集地址供机床引用
            TestDb.Execute(@"INSERT INTO cnc_collect_address (id, name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
                VALUES (1, '测试地址', 'http://10.1.1.1/', 1, 30, 1, NOW(), NOW())");
        }

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

        private Machine CreateTestMachine(string deviceCode = "TEST_001")
        {
            return new Machine
            {
                DeviceCode = deviceCode,
                Name = "测试机床",
                WorkshopId = 1,
                CollectAddressId = 1,
                BrandId = 1,
                IpAddress = "10.1.1.8",
                IsEnabled = 1,
                CreatedAt = DateTime.Now,
                UpdatedAt = DateTime.Now
            };
        }

        [Fact]
        public void Create_新增机床_返回自增ID()
        {
            var entity = CreateTestMachine();
            var id = _repo.Create(entity);
            Assert.True(id > 0);
        }

        [Fact]
        public void GetById_存在的ID_返回机床实体()
        {
            var id = _repo.Create(CreateTestMachine());
            var result = _repo.GetById(id);
            Assert.NotNull(result);
            Assert.Equal("TEST_001", result.DeviceCode);
            Assert.Equal("测试机床", result.Name);
        }

        [Fact]
        public void GetById_不存在的ID_返回null()
        {
            var result = _repo.GetById(99999);
            Assert.Null(result);
        }

        [Fact]
        public void GetByDeviceCode_存在的编码_返回机床()
        {
            _repo.Create(CreateTestMachine("UNIQUE_CODE"));
            var result = _repo.GetByDeviceCode("UNIQUE_CODE");
            Assert.NotNull(result);
            Assert.Equal("UNIQUE_CODE", result.DeviceCode);
        }

        [Fact]
        public void GetList_分页查询_返回分页结果()
        {
            // 插入几条数据
            for (int i = 1; i <= 3; i++)
            {
                _repo.Create(CreateTestMachine($"CODE_{i:D3}"));
            }
            var query = new MachineQuery { Page = 1, PageSize = 2 };
            var result = _repo.GetList(query);
            Assert.True(result.Total >= 3);
            Assert.True(result.Items.Count <= 2);
        }

        [Fact]
        public void Update_修改机床_返回true()
        {
            var id = _repo.Create(CreateTestMachine());
            var entity = _repo.GetById(id);
            entity.Name = "修改后";
            entity.UpdatedAt = DateTime.Now;
            var result = _repo.Update(entity);
            Assert.True(result);
            Assert.Equal("修改后", _repo.GetById(id).Name);
        }

        [Fact]
        public void Delete_存在的ID_返回true()
        {
            var id = _repo.Create(CreateTestMachine("DEL_001"));
            var result = _repo.Delete(id);
            Assert.True(result);
            Assert.Null(_repo.GetById(id));
        }

        [Fact]
        public void ToggleEnabled_切换启用状态()
        {
            var id = _repo.Create(CreateTestMachine());
            var before = _repo.GetById(id).IsEnabled;
            _repo.ToggleEnabled(id);
            var after = _repo.GetById(id).IsEnabled;
            Assert.NotEqual(before, after);
        }

        [Fact]
        public void GetEnabledByAddressId_返回该地址下启用的机床()
        {
            _repo.Create(CreateTestMachine("ADDR_001"));
            var result = _repo.GetEnabledByAddressId(1);
            Assert.NotNull(result);
            Assert.True(result.Count > 0);
        }
    }
}
