<?php
namespace boru\ai\Schema;

class ToolCall
{
    /** @var string */
    protected $id;

    /** @var string */
    protected $name;

    /** @var array */
    protected $arguments = array();

    /** @var string */
    protected $argumentsJson = '';

    /** @var string */
    protected $status = 'pending';

    /** @var array */
    protected $metadata = array();

    public function id($value = null)
    {
        if ($value !== null) {
            $this->id = (string)$value;
            return $this;
        }
        return $this->id;
    }

    public function name($value = null)
    {
        if ($value !== null) {
            $this->name = (string)$value;
            return $this;
        }
        return $this->name;
    }

    public function arguments($value = null)
    {
        if ($value !== null) {
            if (!is_array($value)) {
                throw new \InvalidArgumentException('ToolCall arguments must be an array');
            }
            $this->arguments = $value;
            return $this;
        }
        return $this->arguments;
    }

    public function argumentsJson($value = null)
    {
        if ($value !== null) {
            $this->argumentsJson = (string)$value;
            return $this;
        }
        if ($this->argumentsJson === '' && !empty($this->arguments)) {
            return json_encode($this->arguments);
        }
        return $this->argumentsJson;
    }

    public function status($value = null)
    {
        if ($value !== null) {
            $this->status = (string)$value;
            return $this;
        }
        return $this->status;
    }

    public function metadata($key = null, $value = null)
    {
        if ($key === null) {
            return $this->metadata;
        }
        if (is_array($key)) {
            $this->metadata = $key;
            return $this;
        }
        if (func_num_args() > 1) {
            $this->metadata[$key] = $value;
            return $this;
        }
        return isset($this->metadata[$key]) ? $this->metadata[$key] : null;
    }

    public function toArray()
    {
        return array(
            'id' => $this->id(),
            'name' => $this->name(),
            'arguments' => $this->arguments(),
            'arguments_json' => $this->argumentsJson(),
            'status' => $this->status(),
            'metadata' => $this->metadata(),
        );
    }
}
