<?php
namespace boru\ai\Schema;

class ContentPart
{
    /** @var string */
    protected $type = 'text';

    /** @var mixed */
    protected $value;

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

    public function __construct($type = 'text', $value = null, $metadata = array())
    {
        $this->type = (string)$type;
        $this->value = $value;
        if (is_array($metadata)) {
            $this->metadata = $metadata;
        }
    }

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

    public function value($value = null)
    {
        if (func_num_args() > 0) {
            $this->value = $value;
            return $this;
        }
        return $this->value;
    }

    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(
            'type' => $this->type(),
            'value' => $this->value(),
            'metadata' => $this->metadata(),
        );
    }
}
