<?php

declare(strict_types=1);

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

namespace App\Tools\Product;

use App\Business\Product\ProductReadQuery;
use App\Services\DberpPermissionService;
use App\Tools\Contracts\Tool;
use App\Tools\Entity\EntityReferenceSchema;
use App\Tools\Enums\ToolRisk;
use App\Tools\ToolExecutionContext;
use App\Tools\ToolMetadata;
use App\Tools\ToolPermission;

final class ProductGetTool implements Tool
{
    public function __construct(
        private readonly ProductReadQuery $query,
    ) {}

    public function metadata(): ToolMetadata
    {
        $skuSchema = [
            'type' => 'object',
            'required' => [
                'id',
                'sku_code',
                'is_enabled',
                'is_default',
                'sales_price',
            ],
            'additionalProperties' => false,
            'properties' => [
                'id' => ['type' => 'integer'],
                'sku_code' => ['type' => 'string'],
                'barcode' => ['type' => 'string'],
                'spec_text' => ['type' => 'string'],
                'is_enabled' => ['type' => 'boolean'],
                'is_default' => ['type' => 'boolean'],
                'sales_price' => ['type' => 'string'],
            ],
        ];

        return new ToolMetadata(
            name: 'product.get',
            version: 1,
            description: '按商品 id 或编码读取商品档案详情（只读）。',
            permission: ToolPermission::permission(DberpPermissionService::VIEW_PRODUCT),
            risk: ToolRisk::ReadOnly,
            inputSchema: [
                'type' => 'object',
                'additionalProperties' => false,
                'properties' => [
                    'id' => ['type' => 'integer', 'minimum' => 1],
                    'code' => [
                        'type' => 'string',
                        'minLength' => 1,
                        'maxLength' => 64,
                    ],
                ],
            ],
            outputSchema: EntityReferenceSchema::withRef([
                'type' => 'object',
                'required' => [
                    'id',
                    'code',
                    'name',
                    'unit_id',
                    'is_enabled',
                    'can_sale',
                    'can_purchase',
                    'sales_price',
                    'skus',
                ],
                'additionalProperties' => false,
                'properties' => [
                    'id' => ['type' => 'integer'],
                    'code' => ['type' => 'string'],
                    'name' => ['type' => 'string'],
                    'barcode' => ['type' => 'string'],
                    'mnemonic_code' => ['type' => 'string'],
                    'category_id' => ['type' => 'integer'],
                    'category_name' => ['type' => 'string'],
                    'brand_name' => ['type' => 'string'],
                    'spec_model' => ['type' => 'string'],
                    'unit_id' => ['type' => 'integer'],
                    'unit_name' => ['type' => 'string'],
                    'is_enabled' => ['type' => 'boolean'],
                    'can_sale' => ['type' => 'boolean'],
                    'can_purchase' => ['type' => 'boolean'],
                    'sales_price' => ['type' => 'string'],
                    'skus' => [
                        'type' => 'array',
                        'items' => $skuSchema,
                    ],
                ],
            ]),
            owner: 'core',
        );
    }

    /**
     * @param  array<string, mixed>  $input
     * @return array<string, mixed>
     */
    public function execute(ToolExecutionContext $context, array $input): array
    {
        return $this->query->find($input);
    }
}
