using System;
using Newtonsoft.Json.Linq;


namespace AIRuntime.Runtime.Tools
{
    public sealed class ToolCallParser
    {
        public ToolCallRequest Parse(
            string json)
        {
            if(string.IsNullOrWhiteSpace(json))
            {
                throw new ArgumentException(
                    "Tool call JSON is empty.");
            }


            JObject root =
                JObject.Parse(json);


            string name =
                root["name"]?
                    .Value<string>();


            if(string.IsNullOrWhiteSpace(name))
            {
                throw new InvalidOperationException(
                    "Tool call missing name.");
            }


            JObject arguments =
                root["arguments"] as JObject
                ?? new JObject();


            return new ToolCallRequest(
                name,
                arguments);
        }
    }
}