<?php
namespace boru\ai\Conversation;

use boru\ai\Schema\Request;
use boru\ai\Schema\Response;
use boru\ai\Schema\ToolCall;
use boru\ai\Schema\ToolResult;

class Turn
{
    /** @var string|null */
    protected $id;

    /** @var int|null */
    protected $number;

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

    /** @var Request|null */
    protected $request;

    /** @var Response|null */
    protected $response;

    /** @var ToolCall[] */
    protected $toolCalls = array();

    /** @var ToolResult[] */
    protected $toolResults = array();

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

    /** @var string|null */
    protected $startedAt;

    /** @var string|null */
    protected $completedAt;

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

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

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

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

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

    public function addToolCall(ToolCall $toolCall)
    {
        $this->toolCalls[] = $toolCall;
        return $this;
    }

    public function toolCalls($value = null)
    {
        if ($value !== null) {
            $this->toolCalls = array();
            foreach ((array)$value as $toolCall) {
                if ($toolCall instanceof ToolCall) {
                    $this->toolCalls[] = $toolCall;
                }
            }
            return $this;
        }
        return $this->toolCalls;
    }

    public function addToolResult(ToolResult $toolResult)
    {
        $this->toolResults[] = $toolResult;
        return $this;
    }

    public function toolResults($value = null)
    {
        if ($value !== null) {
            $this->toolResults = array();
            foreach ((array)$value as $toolResult) {
                if ($toolResult instanceof ToolResult) {
                    $this->toolResults[] = $toolResult;
                }
            }
            return $this;
        }
        return $this->toolResults;
    }

    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 startedAt($value = null)
    {
        if ($value !== null) {
            $this->startedAt = (string)$value;
            return $this;
        }
        return $this->startedAt;
    }

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

    public function toArray()
    {
        $toolCalls = array();
        foreach ($this->toolCalls() as $toolCall) {
            $toolCalls[] = $toolCall->toArray();
        }

        $toolResults = array();
        foreach ($this->toolResults() as $toolResult) {
            $toolResults[] = $toolResult->toArray();
        }

        return array(
            'id' => $this->id(),
            'number' => $this->number(),
            'status' => $this->status(),
            'request' => $this->request() ? $this->request()->toArray() : null,
            'response' => $this->response() ? $this->response()->toArray() : null,
            'tool_calls' => $toolCalls,
            'tool_results' => $toolResults,
            'metadata' => $this->metadata(),
            'started_at' => $this->startedAt(),
            'completed_at' => $this->completedAt(),
        );
    }
}
