<?php

/*
 * This file is part of the official PHP MCP SDK.
 *
 * A collaboration between Symfony and the PHP Foundation.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
namespace Matomo\Dependencies\McpServer\Mcp\Schema\JsonRpc;

use Matomo\Dependencies\McpServer\Mcp\Exception\InvalidArgumentException;
/**
 * @template TResult
 *
 * @phpstan-type ResponseData array{
 *     jsonrpc: string,
 *     id: string|int,
 *     result: array<string, mixed>,
 * }
 *
 * @author Kyrian Obikwelu <koshnawaza@gmail.com>
 */
class Response implements MessageInterface
{
    /**
     * @param string|int $id     this MUST be the same as the value of the id member in the Request Object
     * @param TResult    $result the value of this member is determined by the method invoked on the Server
     */
    public function __construct(
        public readonly string|int $id,
        /** @var TResult */
        public readonly mixed $result
    )
    {
    }
    public function getId() : string|int
    {
        return $this->id;
    }
    /**
     * @param ResponseData $data
     *
     * @return self<array<string, mixed>>
     */
    public static function fromArray(array $data) : self
    {
        if (($data['jsonrpc'] ?? null) !== MessageInterface::JSONRPC_VERSION) {
            throw new InvalidArgumentException('Invalid or missing "jsonrpc" version for Response.');
        }
        if (!isset($data['id'])) {
            throw new InvalidArgumentException('Missing "id" for Response.');
        }
        if (!\is_string($data['id']) && !\is_int($data['id'])) {
            throw new InvalidArgumentException('Invalid "id" type for Response.');
        }
        if (!isset($data['result'])) {
            throw new InvalidArgumentException('Response must contain "result" field.');
        }
        if (!\is_array($data['result'])) {
            throw new InvalidArgumentException('Response "result" must be an array.');
        }
        return new self($data['id'], $data['result']);
    }
    /**
     * @return array{
     *     jsonrpc: string,
     *     id: string|int,
     *     result: mixed,
     * }
     */
    public function jsonSerialize() : array
    {
        return ['jsonrpc' => MessageInterface::JSONRPC_VERSION, 'id' => $this->id, 'result' => $this->result];
    }
}
