<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Tools;

/**
 * 权限感知发现结果。仅含 metadata，不含 Tool 实例，不可执行。
 */
final readonly class ToolDiscoveryDescriptor
{
    /**
     * @param  array{type: string, value: string}  $permission
     * @param  array<string, mixed>  $inputSchema
     * @param  array<string, mixed>  $outputSchema
     */
    public function __construct(
        public string $name,
        public int $version,
        public string $description,
        public array $permission,
        public string $risk,
        public array $inputSchema,
        public array $outputSchema,
        public string $owner,
    ) {}

    public static function fromMetadata(ToolMetadata $metadata): self
    {
        $payload = $metadata->toArray();

        return new self(
            name: $payload['name'],
            version: $payload['version'],
            description: $payload['description'],
            permission: $payload['permission'],
            risk: $payload['risk'],
            inputSchema: $payload['input_schema'],
            outputSchema: $payload['output_schema'],
            owner: $payload['owner'],
        );
    }

    /**
     * @return array{
     *     name: string,
     *     version: int,
     *     description: string,
     *     permission: array{type: string, value: string},
     *     risk: string,
     *     input_schema: array<string, mixed>,
     *     output_schema: array<string, mixed>,
     *     owner: string
     * }
     */
    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'version' => $this->version,
            'description' => $this->description,
            'permission' => $this->permission,
            'risk' => $this->risk,
            'input_schema' => $this->inputSchema,
            'output_schema' => $this->outputSchema,
            'owner' => $this->owner,
        ];
    }
}
