using System;
using System.Linq;
using UnityEditor;
using Object = UnityEngine.Object;

namespace LLM.Editor.Analysis
{
    /// <summary>
    /// Executes an AssetDatabase.FindAssets query and returns the results.
    /// </summary>
    public class FindAssetsProvider : IContextProvider
    {
        public object GetContext(Object target, string qualifier)
        {
            if (string.IsNullOrEmpty(qualifier))
            {
                return "Error: A query string (qualifier) is required for FindAssets.";
            }

            var guids = AssetDatabase.FindAssets(qualifier);
            if (guids != null && guids.Length != 0)
            {
                return guids.Select(guid => new
                {
                    guid,
                    path = AssetDatabase.GUIDToAssetPath(guid)
                }).ToArray();
            }
            if (!qualifier.Contains("guid:")) return Array.Empty<object>(); // Return an empty array if nothing is found
            var guid = qualifier.Replace("guid:", "");
            var path = AssetDatabase.GUIDToAssetPath(guid);
            return !string.IsNullOrEmpty(path) ? new [] { new { guid, path } } : Array.Empty<object>();
        }
    }
}