<?php

declare(strict_types=1);

namespace Anthropic\Beta\Messages;

use Anthropic\Beta\Messages\BetaMCPToolResultBlock\Content;
use Anthropic\Core\Attributes\Required;
use Anthropic\Core\Concerns\SdkModel;
use Anthropic\Core\Contracts\BaseModel;

/**
 * @phpstan-import-type ContentVariants from \Anthropic\Beta\Messages\BetaMCPToolResultBlock\Content
 * @phpstan-import-type ContentShape from \Anthropic\Beta\Messages\BetaMCPToolResultBlock\Content
 *
 * @phpstan-type BetaMCPToolResultBlockShape = array{
 *   content: ContentShape,
 *   isError: bool,
 *   toolUseID: string,
 *   type: 'mcp_tool_result',
 * }
 */
final class BetaMCPToolResultBlock implements BaseModel
{
    /** @use SdkModel<BetaMCPToolResultBlockShape> */
    use SdkModel;

    /** @var 'mcp_tool_result' $type */
    #[Required]
    public string $type = 'mcp_tool_result';

    /** @var ContentVariants $content */
    #[Required(union: Content::class)]
    public string|array $content;

    #[Required('is_error')]
    public bool $isError;

    #[Required('tool_use_id')]
    public string $toolUseID;

    /**
     * `new BetaMCPToolResultBlock()` is missing required properties by the API.
     *
     * To enforce required parameters use
     * ```
     * BetaMCPToolResultBlock::with(content: ..., isError: ..., toolUseID: ...)
     * ```
     *
     * Otherwise ensure the following setters are called
     *
     * ```
     * (new BetaMCPToolResultBlock)
     *   ->withContent(...)
     *   ->withIsError(...)
     *   ->withToolUseID(...)
     * ```
     */
    public function __construct()
    {
        $this->initialize();
    }

    /**
     * Construct an instance from the required parameters.
     *
     * You must use named parameters to construct any parameters with a default value.
     *
     * @param ContentShape $content
     */
    public static function with(
        string|array $content,
        string $toolUseID,
        bool $isError = false
    ): self {
        $self = new self;

        $self['content'] = $content;
        $self['isError'] = $isError;
        $self['toolUseID'] = $toolUseID;

        return $self;
    }

    /**
     * @param ContentShape $content
     */
    public function withContent(string|array $content): self
    {
        $self = clone $this;
        $self['content'] = $content;

        return $self;
    }

    public function withIsError(bool $isError): self
    {
        $self = clone $this;
        $self['isError'] = $isError;

        return $self;
    }

    public function withToolUseID(string $toolUseID): self
    {
        $self = clone $this;
        $self['toolUseID'] = $toolUseID;

        return $self;
    }

    /**
     * @param 'mcp_tool_result' $type
     */
    public function withType(string $type): self
    {
        $self = clone $this;
        $self['type'] = $type;

        return $self;
    }
}
