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

namespace CncRepository.Tests
{
    /// <summary>
    /// 告警仓储测试
    /// </summary>
    [Collection("Database")]
    public class AlertRepositoryTests : IDisposable
    {
        private readonly AlertRepository _repo;

        public AlertRepositoryTests()
        {
            _repo = new AlertRepository(TestDb.ConnectionString);
            TestDb.TruncateAll();
        }

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

        private Alert CreateTestAlert(string alertType = "collect_fail")
        {
            return new Alert
            {
                AlertType = alertType,
                Title = "测试告警",
                MachineId = null,
                CollectAddressId = null,
                Detail = "测试详情",
                IsResolved = 0,
                CreatedAt = DateTime.Now
            };
        }

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

        [Fact]
        public void GetById_存在的ID_返回告警()
        {
            var id = _repo.Create(CreateTestAlert());
            var result = _repo.GetById(id);
            Assert.NotNull(result);
            Assert.Equal("collect_fail", result.AlertType);
            Assert.Equal(0, result.IsResolved);
        }

        [Fact]
        public void GetList_分页查询_返回分页结果()
        {
            for (int i = 0; i < 5; i++)
                _repo.Create(CreateTestAlert());
            var query = new AlertQuery { Page = 1, PageSize = 3 };
            var result = _repo.GetList(query);
            Assert.Equal(5, result.Total);
            Assert.Equal(3, result.Items.Count);
        }

        [Fact]
        public void Resolve_处理告警_返回true()
        {
            var id = _repo.Create(CreateTestAlert());
            var result = _repo.Resolve(id);
            Assert.True(result);
            var alert = _repo.GetById(id);
            Assert.Equal(1, alert.IsResolved);
        }

        [Fact]
        public void BatchResolve_批量处理_返回处理数量()
        {
            var id1 = _repo.Create(CreateTestAlert());
            var id2 = _repo.Create(CreateTestAlert());
            var id3 = _repo.Create(CreateTestAlert());
            var count = _repo.BatchResolve(new System.Collections.Generic.List<long> { id1, id2 });
            Assert.Equal(2, count);
            Assert.Equal(1, _repo.GetById(id1).IsResolved);
            Assert.Equal(1, _repo.GetById(id2).IsResolved);
            Assert.Equal(0, _repo.GetById(id3).IsResolved);
        }

        [Fact]
        public void GetStatistics_返回未处理统计()
        {
            _repo.Create(CreateTestAlert("collect_fail"));
            _repo.Create(CreateTestAlert("collect_fail"));
            _repo.Create(CreateTestAlert("device_offline"));
            var stats = _repo.GetStatistics();
            Assert.True(stats.UnresolvedCount >= 3);
            Assert.True(stats.UnresolvedByType.ContainsKey("collect_fail"));
            Assert.True(stats.UnresolvedByType["collect_fail"] >= 2);
        }
    }
}
