using UnityEditor;
using System.Collections.Generic;
using Object = UnityEngine.Object;

namespace LLM.Editor.Analysis
{
    /// <summary>
    /// Provides context about the currently selected objects in the Unity Editor.
    /// Distinguishes between objects in the scene and assets in the project.
    /// </summary>
    public class SelectionProvider : IContextProvider
    {
        public class SelectionResult
        {
            public string name;
            public string id;
            public string type;
            public bool isSceneObject;
            public string assetPath;
        }

        public object GetContext(Object target, string qualifier)
        {
            var selectedObjects = Selection.objects;

            if (selectedObjects == null || selectedObjects.Length == 0)
            {
                return "No objects are currently selected in the editor.";
            }

            var results = new List<SelectionResult>();

            foreach (var obj in selectedObjects)
            {
                var result = new SelectionResult
                {
                    name = obj.name,
                    type = obj.GetType().FullName
                };

                if (AssetDatabase.Contains(obj))
                {
                    // It's an asset in the Project window
                    result.isSceneObject = false;
                    var path = AssetDatabase.GetAssetPath(obj);
                    result.assetPath = path;
                    result.id = AssetDatabase.AssetPathToGUID(path);
                }
                else
                {
                    // It's a GameObject or component in the Scene
                    result.isSceneObject = true;
                    result.assetPath = null;
                    result.id = obj.GetInstanceID().ToString();
                }
                results.Add(result);
            }

            return results;
        }
    }
}
