using System;
using System.Collections.Generic;
using Xunit;
using CncCollector.Core;
using CncModels.Entity;

namespace CncCollector.Tests
{
    /// <summary>
    /// CollectRecordWriter 单元测试。
    /// CollectRecordWriter 使用静态方法 + MySqlConnection，
    /// 使用无效连接字符串验证容错行为（不抛异常）。
    /// </summary>
    public class CollectRecordWriterTests
    {
        /// <summary>无效连接字符串</summary>
        private const string InvalidConnStr = "Server=invalid;Database=invalid;User Id=invalid;Password=invalid;";

        // ===== WriteBatch 测试 =====

        [Fact]
        public void WriteBatch_无效连接字符串_不抛异常()
        {
            // WriteBatch 内部所有DB操作都有 try-catch，不应向外抛异常
            var ex = Record.Exception(() => CollectRecordWriter.WriteBatch(
                InvalidConnStr, InvalidConnStr,
                null, "", 1, DateTime.Now, 100L, false, "测试错误"));
            Assert.Null(ex);
        }

        [Fact]
        public void WriteBatch_成功状态但记录为空_不抛异常()
        {
            var ex = Record.Exception(() => CollectRecordWriter.WriteBatch(
                InvalidConnStr, InvalidConnStr,
                new List<CollectRecord>(), "{}", 1, DateTime.Now, 50L, true, null));
            Assert.Null(ex);
        }

        [Fact]
        public void WriteBatch_成功状态有记录_不抛异常()
        {
            var records = new List<CollectRecord>
            {
                new CollectRecord
                {
                    MachineId = 1,
                    CollectTime = DateTime.Now,
                    ProgramName = "O0001",
                    PartCount = 10
                }
            };
            var ex = Record.Exception(() => CollectRecordWriter.WriteBatch(
                InvalidConnStr, InvalidConnStr,
                records, "{}", 1, DateTime.Now, 200L, true, null));
            Assert.Null(ex);
        }

        [Fact]
        public void WriteBatch_失败状态_不抛异常()
        {
            var ex = Record.Exception(() => CollectRecordWriter.WriteBatch(
                InvalidConnStr, InvalidConnStr,
                null, "原始JSON", 1, DateTime.Now, null, false, "连接超时"));
            Assert.Null(ex);
        }

        [Fact]
        public void WriteBatch_null连接字符串_不抛异常()
        {
            // null连接字符串会在 MySqlConnection 构造时失败，但应被 try-catch 捕获
            var ex = Record.Exception(() => CollectRecordWriter.WriteBatch(
                null, null,
                null, "", 1, DateTime.Now, null, false, "错误"));
            Assert.Null(ex);
        }

        [Fact]
        public void WriteBatch_null原始JSON_不抛异常()
        {
            var ex = Record.Exception(() => CollectRecordWriter.WriteBatch(
                InvalidConnStr, InvalidConnStr,
                null, null, 1, DateTime.Now, 100L, false, null));
            Assert.Null(ex);
        }

        [Fact]
        public void WriteBatch_多条记录_不抛异常()
        {
            var records = new List<CollectRecord>
            {
                new CollectRecord { MachineId = 1, CollectTime = DateTime.Now, ProgramName = "O0001", PartCount = 5 },
                new CollectRecord { MachineId = 2, CollectTime = DateTime.Now, ProgramName = "O0002", PartCount = 10 },
                new CollectRecord { MachineId = 3, CollectTime = DateTime.Now, ProgramName = "O0003", PartCount = 15 }
            };
            var ex = Record.Exception(() => CollectRecordWriter.WriteBatch(
                InvalidConnStr, InvalidConnStr,
                records, "[]", 1, DateTime.Now, 300L, true, null));
            Assert.Null(ex);
        }

        // ===== RecordFailure 测试 =====

        [Fact]
        public void RecordFailure_无效连接字符串_不抛异常()
        {
            var ex = Record.Exception(() => CollectRecordWriter.RecordFailure(
                InvalidConnStr, 1, "连接超时"));
            Assert.Null(ex);
        }

        [Fact]
        public void RecordFailure_null连接字符串_不抛异常()
        {
            var ex = Record.Exception(() => CollectRecordWriter.RecordFailure(
                null, 1, "错误"));
            Assert.Null(ex);
        }

        [Fact]
        public void RecordFailure_null错误信息_不抛异常()
        {
            var ex = Record.Exception(() => CollectRecordWriter.RecordFailure(
                InvalidConnStr, 1, null));
            Assert.Null(ex);
        }

        [Fact]
        public void RecordFailure_无效地址ID_不抛异常()
        {
            var ex = Record.Exception(() => CollectRecordWriter.RecordFailure(
                InvalidConnStr, -1, "地址无效"));
            Assert.Null(ex);
        }
    }
}
