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

namespace CncService.Tests
{
    /// <summary>
    /// CollectDataService 采集数据查询测试
    /// 测试场景：分页查询原始记录、获取最新记录、参数校验
    /// </summary>
    [Collection("Database")]
    public class CollectDataServiceTests : IDisposable
    {
        private readonly CollectDataService _service;

        public CollectDataServiceTests()
        {
            TestDb.TruncateAll();
            _service = ServiceFactory.CreateCollectDataService();
        }

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

        // ======== GetRawByAddress ========

        [Fact]
        public void GetRawByAddress_无效地址ID_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.GetRawByAddress(0, 1, 20));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void GetRawByAddress_负数地址ID_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.GetRawByAddress(-1, 1, 20));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void GetRawByAddress_无效页码_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.GetRawByAddress(1, 0, 20));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void GetRawByAddress_无效页大小_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.GetRawByAddress(1, 1, 0));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void GetRawByAddress_无数据_返回空分页()
        {
            // 先插入一个采集地址
            TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
                VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");

            var result = _service.GetRawByAddress(1, 1, 20);
            Assert.NotNull(result);
            Assert.Equal(0, result.Total);
        }

        // ======== GetLatestRaw ========

        [Fact]
        public void GetLatestRaw_无效地址ID_抛出BadRequest异常()
        {
            var ex = Assert.Throws<BusinessException>(() => _service.GetLatestRaw(0));
            Assert.Equal(ErrorCode.BadRequest, ex.Code);
        }

        [Fact]
        public void GetLatestRaw_无数据_返回null()
        {
            TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
                VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");

            var result = _service.GetLatestRaw(1);
            Assert.Null(result);
        }

        [Fact]
        public void GetLatestRaw_有数据_返回最新记录()
        {
            TestDb.Execute(@"INSERT INTO cnc_collect_address (name, url, brand_id, collect_interval, is_enabled, created_at, updated_at)
                VALUES ('测试地址', 'http://test', 1, 30, 1, NOW(), NOW())");
            TestDb.Execute("INSERT INTO log_collect_raw (collect_address_id, request_time, response_time, is_success, raw_json, created_at) VALUES (1, NOW(), NOW(), 1, '{\"\"test\"\":1}', NOW())");

            var result = _service.GetLatestRaw(1);
            Assert.NotNull(result);
        }
    }
}
