using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;

namespace NavisworksTransport.UnitTests.Integration
{
    /// <summary>
    /// 批量任务队列集成测试（覆盖计划 T14，轻量版）。
    ///
    /// 验证 BatchQueueManager 队列管理：
    /// - 添加队列项返回有效 itemId
    /// - 队列计数随添加递增
    /// - 动画参数（帧率/时长）随队列项传递
    ///
    /// 说明：不执行 ProcessQueueAsync（批量动画执行链路长且耗时），队列处理链路另行验证。
    /// </summary>
    [TestClass]
    [TestCategory("NavisworksIntegration")]
    public class BatchQueueAutomationTests
    {
        private const int TestFrameRate = 15;
        private const double TestDurationSeconds = 5.0;

        [TestMethod]
        [Timeout(120000)]
        public async Task BatchQueue_AddItems_QueueCountIncrements()
        {
            using (var client = new NavisworksTestAutomationClient())
            {
                await client.EnsureServiceReadyAsync(TimeSpan.FromSeconds(90)).ConfigureAwait(false);

                // 1. 添加第一个队列项
                JObject first = await client.BatchQueueAddAsync(
                    "自动测试_Ground", TestFrameRate, TestDurationSeconds).ConfigureAwait(false);
                Assert.IsTrue(first.Value<bool>("ok"), "添加队列项失败: " + (string)first["error"]);

                JObject firstData = (JObject)first["data"];
                int firstItemId = (int)firstData["itemId"];
                int queueAfterFirst = (int)firstData["queueCount"];
                Assert.IsTrue(firstItemId > 0, "队列项 ID 无效");
                Assert.IsTrue(queueAfterFirst >= 1, "添加后队列计数应 >= 1");
                Assert.AreEqual("自动测试_Ground", (string)firstData["routeName"], "队列项路径名不匹配");

                // 2. 添加第二个队列项，队列计数应递增
                JObject second = await client.BatchQueueAddAsync(
                    "自动测试_Hoisting", TestFrameRate, TestDurationSeconds).ConfigureAwait(false);
                Assert.IsTrue(second.Value<bool>("ok"), "添加第二个队列项失败: " + (string)second["error"]);

                JObject secondData = (JObject)second["data"];
                Assert.IsTrue((int)secondData["itemId"] > firstItemId, "第二个队列项 ID 应递增");
                Assert.IsTrue((int)secondData["queueCount"] > queueAfterFirst, "队列计数应随添加递增");
                Assert.IsFalse((bool)secondData["isExecuting"], "仅添加不应触发执行");
            }
        }
    }
}
