<?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\ProductSearchQuery;
use App\Services\DberpPermissionService;
use App\Tools\Contracts\Tool;
use App\Tools\Entity\EntityReferenceSchema;
use App\Tools\Enums\ToolRisk;
use App\Tools\Support\ToolListSchema;
use App\Tools\ToolExecutionContext;
use App\Tools\ToolMetadata;
use App\Tools\ToolPermission;

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

    public function metadata(): ToolMetadata
    {
        $itemSchema = [
            'type' => 'object',
            'required' => [
                'id',
                'code',
                'name',
                'unit_id',
                'is_enabled',
                'can_sale',
                'can_purchase',
                'sales_price',
            ],
            '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'],
            ],
        ];

        return new ToolMetadata(
            name: 'product.search',
            version: 1,
            description: '按编码、名称、条码、助记码检索商品档案（只读）。',
            permission: ToolPermission::permission(DberpPermissionService::VIEW_PRODUCT),
            risk: ToolRisk::ReadOnly,
            inputSchema: [
                'type' => 'object',
                'additionalProperties' => false,
                'properties' => [
                    'q' => [
                        'type' => 'string',
                        'minLength' => 1,
                        'maxLength' => 100,
                    ],
                    'is_enabled' => ['type' => 'boolean'],
                    'can_sale' => ['type' => 'boolean'],
                    'can_purchase' => ['type' => 'boolean'],
                    'category_id' => [
                        'type' => 'integer',
                        'minimum' => 1,
                    ],
                    ...ToolListSchema::paginationInputProperties(
                        ProductSearchQuery::SORTABLE,
                        ProductSearchQuery::DEFAULT_SORT,
                    ),
                ],
            ],
            outputSchema: ToolListSchema::paginatedOutputSchema(EntityReferenceSchema::withRef($itemSchema)),
            owner: 'core',
        );
    }

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