using NUnit.Framework;
using UnityEngine;
using UnityEditor;
using System.IO;
using LLM.Editor.Analysis;
using System.Linq;

namespace LLM.Editor.Tests.Unit
{
    [TestFixture]
    public class FindAssetsProviderTests
    {
        private const string TestFolderName = "TempFindAssetsTest";
        private string _testRootPath;
        private string _testMaterialPath;
        private string _testScriptPath;

        [SetUp]
        public void SetUp()
        {
            _testRootPath = Path.Combine(Application.dataPath, TestFolderName);
            Directory.CreateDirectory(_testRootPath);

            // Create a test material
            _testMaterialPath = $"Assets/{TestFolderName}/MyTestMaterial.mat";
            AssetDatabase.CreateAsset(new Material(Shader.Find("Standard")), _testMaterialPath);

            // Create a test script
            _testScriptPath = Path.Combine(_testRootPath, "MyTestScript.cs");
            File.WriteAllText(_testScriptPath, "public class MyTestScript {}");
            
            AssetDatabase.Refresh();
        }

        [TearDown]
        public void TearDown()
        {
            if (Directory.Exists(_testRootPath))
            {
                Directory.Delete(_testRootPath, true);
                File.Delete(_testRootPath + ".meta");
                AssetDatabase.Refresh();
            }
        }

        [Test]
        public void GetContext_WithStandardQuery_FindsAssets()
        {
            // Arrange
            var provider = new FindAssetsProvider();
            var query = $"MyTestMaterial t:Material";

            // Act
            var context = provider.GetContext(null, query);
            
            // Assert
            Assert.IsInstanceOf<FindAssetsProvider.AssetInfo[]>(context);
            var results = (FindAssetsProvider.AssetInfo[])context;
            Assert.AreEqual(1, results.Length);
            Assert.AreEqual("MyTestMaterial.mat", results[0].name);
        }

        [Test]
        public void GetContext_WithGlobPattern_FindsAssets()
        {
            // Arrange
            var provider = new FindAssetsProvider();
            var globPattern = $"{TestFolderName}/*.cs";

            // Act
            var context = provider.GetContext(null, globPattern);

            // Assert
            Assert.IsInstanceOf<FindAssetsProvider.AssetInfo[]>(context);
            var results = (FindAssetsProvider.AssetInfo[])context;
            Assert.AreEqual(1, results.Length);
            Assert.AreEqual("MyTestScript.cs", results[0].name);
        }

        [Test]
        public void GetContext_WithNoResultsQuery_ReturnsEmptyArray()
        {
            // Arrange
            var provider = new FindAssetsProvider();
            var query = "NonExistentAsset t:Texture";

            // Act
            var context = provider.GetContext(null, query);
            
            // Assert
            Assert.IsInstanceOf<FindAssetsProvider.AssetInfo[]>(context);
            var results = (FindAssetsProvider.AssetInfo[])context;
            Assert.AreEqual(0, results.Length);
        }

        [Test]
        public void GetContext_WithEmptyQualifier_ReturnsError()
        {
            // Arrange
            var provider = new FindAssetsProvider();

            // Act
            var context = provider.GetContext(null, "");

            // Assert
            Assert.IsInstanceOf<string>(context);
            Assert.IsTrue(((string)context).Contains("A query string (qualifier) is required"));
        }
    }
}