using System;
using CncModels.Constants;
using CncModels.Dto;
using CncModels.Dto.Log;
using CncService;
using CncService.Impl;
using Xunit;

namespace CncService.Tests
{
    /// <summary>
    /// SystemLogService 系统日志测试
    /// 测试场景：分页查询、参数校验
    /// </summary>
    [Collection("Database")]
    public class SystemLogServiceTests : IDisposable
    {
        private readonly SystemLogService _service;

        public SystemLogServiceTests()
        {
            TestDb.TruncateAll();
            _service = ServiceFactory.CreateSystemLogService();
        }

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

        [Fact]
        public void GetList_无数据_返回空列表()
        {
            var result = _service.GetList(new SystemLogQuery { Page = 1, PageSize = 20 });
            Assert.NotNull(result);
            Assert.Equal(0, result.Total);
        }

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

        [Fact]
        public void GetList_有日志数据_返回分页结果()
        {
            TestDb.Execute(@"INSERT INTO log_system (log_level, source, message, stack_trace, extra_data, created_at)
                VALUES ('INFO', 'AuthService', '登录系统', '', '{}', NOW())");

            var result = _service.GetList(new SystemLogQuery { Page = 1, PageSize = 20 });
            Assert.Equal(1, result.Total);
        }
    }
}
