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

namespace LLM.Editor.Commands
{
    /// <summary>
    /// A command that does not act on the scene directly, but instead signals the UI
    /// to prompt the user for the objects needed for an analysis.
    /// </summary>
    [UsedImplicitly]
    public class RequestAnalysisContextCommand : ICommand
    {
        // This event can be subscribed to by the MCPWindow to update its UI.
        public static event System.Action<RequestAnalysisContextParams> OnAnalysisContextRequested;

        private readonly RequestAnalysisContextParams _params;

        public RequestAnalysisContextCommand(string jsonParams)
        {
            _params = jsonParams?.FromJson<RequestAnalysisContextParams>();
        }

        public CommandOutcome Execute(Data.CommandContext context)
        {
            if (_params?.subjectRoles == null || _params.subjectRoles.Length == 0)
            {
                Debug.LogError("[RequestAnalysisContextCommand] Invalid parameters. Subject roles are required.");
                return CommandOutcome.Error;
            }

            // The command's job is to simply invoke the event.
            // The UI is responsible for handling it and showing the staging area.
            OnAnalysisContextRequested?.Invoke(_params);

            Debug.Log($"[RequestAnalysisContextCommand] Fired event to request context for roles: {string.Join(", ", _params.subjectRoles)}");

            return CommandOutcome.Success;
        }
    }
}