using JetBrains.Annotations;
using LLM.Editor.Helper;
using UnityEditor;
using UnityEngine;

namespace LLM.Editor.Commands
{
    [System.Serializable]
    public class RenameGameObjectParams
    {
        public int targetIdentifier;
        public string newName;
    }

    [UsedImplicitly]
    public class RenameGameObjectCommand : ICommand
    {
        private readonly RenameGameObjectParams _params;

        public RenameGameObjectCommand(string jsonParams)
        {
            _params = jsonParams?.FromJson<RenameGameObjectParams>();
        }

        public CommandOutcome Execute(Data.CommandContext context)
        {
            if (_params == null || string.IsNullOrEmpty(_params.newName) || _params.targetIdentifier == 0)
            {
                context.ErrorMessage = "Invalid parameters: targetIdentifier and newName are required.";
                Debug.LogError($"[RenameGameObjectCommand] {context.ErrorMessage}");
                return CommandOutcome.Error;
            }

            var targetObject = EditorUtility.InstanceIDToObject(_params.targetIdentifier);
            if (!targetObject)
            {
                context.ErrorMessage = $"Could not find GameObject with ID: {_params.targetIdentifier}";
                Debug.LogError($"[RenameGameObjectCommand] {context.ErrorMessage}");
                return CommandOutcome.Error;
            }

            targetObject.name = _params.newName;
            Debug.Log($"[RenameGameObjectCommand] Renamed object to: {_params.newName}");
            return CommandOutcome.Success;
        }
    }
}