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

namespace LLM.Editor.Tests.Unit
{
    [TestFixture]
    public class FindInSceneProviderTests
    {
        private GameObject _playerObject;
        private GameObject _enemyObject;
        private GameObject _cameraObject;

        [SetUp]
        public void SetUp()
        {
            _playerObject = new GameObject("Player");
            _enemyObject = new GameObject("Enemy_Orc");
            _cameraObject = new GameObject("Main Camera");
            
            _playerObject.AddComponent<Rigidbody>();
            _playerObject.tag = "Player";
            _cameraObject.AddComponent<Camera>();
        }

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

        [Test]
        public void GetContext_FindsByName_Successfully()
        {
            // Arrange
            var provider = new FindInSceneProvider();

            // Act
            var context = provider.GetContext(null, "name: Player");
            
            // Assert
            Assert.IsInstanceOf<List<FindInSceneProvider.SceneQueryResult>>(context);
            var results = (List<FindInSceneProvider.SceneQueryResult>)context;
            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("Player", results[0].name);
        }

        [Test]
        public void GetContext_FindsByNameWithWildcard_Successfully()
        {
            // Arrange
            var provider = new FindInSceneProvider();

            // Act
            var context = provider.GetContext(null, "name: Enemy*");
            
            // Assert
            Assert.IsInstanceOf<List<FindInSceneProvider.SceneQueryResult>>(context);
            var results = (List<FindInSceneProvider.SceneQueryResult>)context;
            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("Enemy_Orc", results[0].name);
        }

        [Test]
        public void GetContext_FindsByComponent_Successfully()
        {
            // Arrange
            var provider = new FindInSceneProvider();

            // Act
            var context = provider.GetContext(null, "component: UnityEngine.Rigidbody");
            
            // Assert
            Assert.IsInstanceOf<List<FindInSceneProvider.SceneQueryResult>>(context);
            var results = (List<FindInSceneProvider.SceneQueryResult>)context;
            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("Player", results[0].name);
        }

        [Test]
        public void GetContext_FindsByTag_Successfully()
        {
            // Arrange
            var provider = new FindInSceneProvider();

            // Act
            var context = provider.GetContext(null, "tag: Player");
            
            // Assert
            Assert.IsInstanceOf<List<FindInSceneProvider.SceneQueryResult>>(context);
            var results = (List<FindInSceneProvider.SceneQueryResult>)context;
            Assert.AreEqual(1, results.Count);
            Assert.AreEqual("Player", results[0].name);
        }

        [Test]
        public void GetContext_WithNoResults_ReturnsEmptyList()
        {
            // Arrange
            var provider = new FindInSceneProvider();

            // Act
            var context = provider.GetContext(null, "name: NonExistentObject");
            
            // Assert
            Assert.IsInstanceOf<List<FindInSceneProvider.SceneQueryResult>>(context);
            var results = (List<FindInSceneProvider.SceneQueryResult>)context;
            Assert.AreEqual(0, results.Count);
        }
    }
}