using System;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using UnityEditor;
using UnityEngine;
using MCPForUnity.Editor.Tools;
using static MCPForUnityTests.Editor.TestUtilities;

namespace MCPForUnityTests.Editor.Tools
{
    public class ManageMaterialReproTests
    {
        private const string TempRoot = "Assets/Temp/ManageMaterialReproTests";
        private string _matPath;

        [SetUp]
        public void SetUp()
        {
            if (!AssetDatabase.IsValidFolder("Assets/Temp"))
            {
                AssetDatabase.CreateFolder("Assets", "Temp");
            }
            if (!AssetDatabase.IsValidFolder(TempRoot))
            {
                AssetDatabase.CreateFolder("Assets/Temp", "ManageMaterialReproTests");
            }

            string guid = Guid.NewGuid().ToString("N");
            _matPath = $"{TempRoot}/ReproMat_{guid}.mat";
        }

        [TearDown]
        public void TearDown()
        {
            if (AssetDatabase.IsValidFolder(TempRoot))
            {
                AssetDatabase.DeleteAsset(TempRoot);
            }

            // Clean up empty parent folders to avoid debris
            CleanupEmptyParentFolders(TempRoot);
        }

        [Test]
        public void CreateMaterial_WithInvalidJsonString_ReturnsGenericError()
        {
            // Arrange
            // Malformed JSON string (missing closing brace)
            string invalidJson = "{\"_Color\": [1,0,0,1]"; 
            
            var paramsObj = new JObject
            {
                ["action"] = "create",
                ["materialPath"] = _matPath,
                ["shader"] = "Standard",
                ["properties"] = invalidJson
            };

            // Act
            var result = ToJObject(ManageMaterial.HandleCommand(paramsObj));

            // Assert
            Assert.IsFalse(result.Value<bool>("success"));
            
            // We expect more detailed error message after fix
            var message = result.Value<string>("error");
            Assert.IsTrue(message.StartsWith("Invalid JSON in properties"), "Message should start with prefix");
            Assert.AreNotEqual("Invalid JSON in properties", message, "Message should contain exception details");
        }
    }
}

