using NUnit.Framework;
using UnityEngine;
using LLM.Editor.Analysis;
using System.Collections.Generic;

namespace LLM.Editor.Tests.Unit
{
    [TestFixture]
    public class ComponentDataProviderTests
    {
        private GameObject _testObject;
        private ComponentDataProvider _provider;

        [SetUp]
        public void SetUp()
        {
            _testObject = new GameObject("TestObject");
            _provider = new ComponentDataProvider();
        }

        [TearDown]
        public void TearDown()
        {
            if (_testObject != null)
            {
                Object.DestroyImmediate(_testObject);
            }
        }

        [Test]
        public void GetContext_WithValidComponent_ReturnsPropertyDictionary()
        {
            // Arrange
            const string qualifier = "UnityEngine.Transform";

            // Act
            var context = _provider.GetContext(_testObject, qualifier);

            // Assert
            Assert.IsInstanceOf<Dictionary<string, object>>(context);
            var data = (Dictionary<string, object>)context;
            
            // Check for some known, serialized properties of the Transform component.
            // The names (e.g., "m_LocalPosition") come from how Unity serializes them.
            Assert.IsTrue(data.ContainsKey("m_LocalPosition"));
            Assert.IsTrue(data.ContainsKey("m_LocalRotation"));
            Assert.IsTrue(data.ContainsKey("m_LocalScale"));
        }

        [Test]
        public void GetContext_WithNonExistentComponent_ReturnsErrorString()
        {
            // Arrange
            var qualifier = "UnityEngine.Rigidbody"; // This component is not on the object

            // Act
            var context = _provider.GetContext(_testObject, qualifier);

            // Assert
            Assert.IsInstanceOf<string>(context);
            Assert.IsTrue(((string)context).Contains("not found on GameObject"));
        }

        [Test]
        public void GetContext_WithInvalidTypeName_ReturnsErrorString()
        {
            // Arrange
            var qualifier = "Invalid.Component.Name";

            // Act
            var context = _provider.GetContext(_testObject, qualifier);

            // Assert
            Assert.IsInstanceOf<string>(context);
            Assert.IsTrue(((string)context).Contains("Component type 'Invalid.Component.Name' not found"));
        }

        [Test]
        public void GetContext_WithNonGameObjectTarget_ReturnsErrorString()
        {
            // Arrange
            var material = new Material(Shader.Find("Standard")); // A non-GameObject target

            // Act
            var context = _provider.GetContext(material, "UnityEngine.Transform");

            // Assert
            Assert.IsInstanceOf<string>(context);
            Assert.IsTrue(((string)context).Contains("Target must be a GameObject"));
        }
    }
}
