using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using LLM.Editor.Commands;
using LLM.Editor.Data;
using LLM.Editor.Helper;

namespace LLM.Editor.Tests.Unit
{
    [TestFixture]
    public class DisplayMessageCommandTests
    {
        private CommandContext _context;

        [SetUp]
        public void SetUp()
        {
            _context = new CommandContext();
        }

        [Test]
        public void Execute_WithSuccessOutcome_LogsInfoMessage()
        {
            // Arrange
            var message = "This is a success message.";
            var parameters = new DisplayMessageParams { outcome = CommandOutcome.Success, message = message };
            var command = new DisplayMessageCommand(parameters.ToJson());

            // Assert that a green log message is expected
            LogAssert.Expect(LogType.Log, $"<color=green>[MCP]: {message}</color>");

            // Act
            var outcome = command.Execute(_context);

            // Assert
            Assert.AreEqual(CommandOutcome.Success, outcome);
        }

        [Test]
        public void Execute_WithErrorOutcome_LogsErrorMessage()
        {
            // Arrange
            var message = "This is an error message.";
            var parameters = new DisplayMessageParams { outcome = CommandOutcome.Error, message = message };
            var command = new DisplayMessageCommand(parameters.ToJson());

            // Assert that a red error log message is expected
            LogAssert.Expect(LogType.Error, $"<color=red>[MCP]: {message}</color>");

            // Act
            var outcome = command.Execute(_context);

            // Assert
            Assert.AreEqual(CommandOutcome.Success, outcome, "The command itself should succeed even when displaying an error.");
        }

        [Test]
        public void Execute_WithEmptyMessage_ReturnsError()
        {
            // Arrange
            var parameters = new DisplayMessageParams { outcome = CommandOutcome.Success, message = "" };
            var command = new DisplayMessageCommand(parameters.ToJson());

            // Assert
            LogAssert.Expect(LogType.Error, "[DisplayMessageCommand] Parameters are invalid or message is empty.");

            // Act
            var outcome = command.Execute(_context);



            // Assert
            Assert.AreEqual(CommandOutcome.Error, outcome);
        }

        [Test]
        public void Execute_WithNullParams_ReturnsError()
        {
            // Arrange
            var command = new DisplayMessageCommand((string)null);

            // Assert
            LogAssert.Expect(LogType.Error, "[DisplayMessageCommand] Parameters are invalid or message is empty.");

            // Act
            var outcome = command.Execute(_context);

            // Assert
            Assert.AreEqual(CommandOutcome.Error, outcome);
        }
    }
}
