using System;
using System.Threading.Tasks;
using AIRuntime.Runtime.Capabilities;


namespace AIRuntime.Runtime.Tools
{
    public sealed class ToolExecutor
    {
        private readonly CapabilityDispatcher _dispatcher;


        public ToolExecutor(
            CapabilityDispatcher dispatcher)
        {
            _dispatcher =
                dispatcher ??
                throw new ArgumentNullException(
                    nameof(dispatcher));
        }


        public async Task<ToolCallResult> ExecuteAsync(
            ToolCallRequest request)
        {
            try
            {
                string result =
                    await _dispatcher.DispatchAsync(
                        new Models.ToolCall
                        {
                            Name = request.Name,
                            Arguments =
                                request.Arguments.ToString()
                        });


                return ToolCallResult.Ok(result);
            }
            catch(Exception exception)
            {
                return ToolCallResult.Fail(
                    exception.Message);
            }
        }
    }
}
