using UnityEditor;
using UnityEngine;
using JetBrains.Annotations;

namespace LLM.Editor.Commands
{
    [UsedImplicitly]
    public class InstantiatePrefabCommand : ICommand
    {
        public InstantiatePrefabCommand(string jsonParams) { } // No params needed for this one

        public CommandOutcome Execute(Data.CommandContext context)
        {
            if (context.CurrentSubject is not string prefabPath)
            {
                Debug.LogError("[InstantiatePrefabCommand] The current subject is not a valid prefab path.");
                return CommandOutcome.Error;
            }

            var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
            var instance = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
            Undo.RegisterCreatedObjectUndo(instance, "Instantiate " + instance.name);
            Selection.activeObject = instance;

            Debug.Log($"[InstantiatePrefabCommand] Instantiated '{prefab.name}' into the scene.");
            context.CurrentSubject = instance;
            return CommandOutcome.Success;
        }
    }
}