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

namespace CncRepository.Tests
{
    /// <summary>
    /// 车间仓储测试
    /// </summary>
    [Collection("Database")]
    public class WorkshopRepositoryTests : IDisposable
    {
        private readonly WorkshopRepository _repo;

        public WorkshopRepositoryTests()
        {
            _repo = new WorkshopRepository(TestDb.ConnectionString);
            TestDb.TruncateAll();
        }

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

        [Fact]
        public void GetAll_返回所有车间()
        {
            var result = _repo.GetAll();
            Assert.NotNull(result);
            Assert.True(result.Count >= 2, "种子数据至少有2个车间");
        }

        [Fact]
        public void GetById_存在的ID_返回车间实体()
        {
            var result = _repo.GetById(1);
            Assert.NotNull(result);
            Assert.Equal("A栋", result.Name);
            Assert.Equal(1, result.SortOrder);
        }

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

        [Fact]
        public void GetList_无关键字_返回分页结果()
        {
            var result = _repo.GetList(null);
            Assert.NotNull(result);
            Assert.True(result.Total >= 2);
            Assert.NotNull(result.Items);
        }

        [Fact]
        public void GetList_关键字搜索_返回匹配结果()
        {
            var result = _repo.GetList("A");
            Assert.True(result.Items.All(w => w.Name.Contains("A")));
        }

        [Fact]
        public void Create_新增车间_返回自增ID()
        {
            var entity = new Workshop
            {
                Name = "C栋",
                SortOrder = 3,
                IsEnabled = 1,
                CreatedAt = DateTime.Now,
                UpdatedAt = DateTime.Now
            };
            var id = _repo.Create(entity);
            Assert.True(id > 0);

            var created = _repo.GetById(id);
            Assert.Equal("C栋", created.Name);
        }

        [Fact]
        public void Update_修改车间_返回true()
        {
            var entity = _repo.GetById(1);
            entity.Name = "A栋-修改";
            entity.UpdatedAt = DateTime.Now;
            var result = _repo.Update(entity);
            Assert.True(result);

            var updated = _repo.GetById(1);
            Assert.Equal("A栋-修改", updated.Name);
        }

        [Fact]
        public void Delete_存在的ID_返回true()
        {
            // 先插入一个独立的车间
            var id = _repo.Create(new Workshop
            {
                Name = "待删除", SortOrder = 99, IsEnabled = 1,
                CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now
            });
            var result = _repo.Delete(id);
            Assert.True(result);
            Assert.Null(_repo.GetById(id));
        }

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

        [Fact]
        public void GetMachineCount_无机床_返回0()
        {
            var count = _repo.GetMachineCount(1);
            Assert.Equal(0, count);
        }
    }
}
