<?php

declare(strict_types=1);

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

namespace App\Tools\MasterData;

use App\Business\MasterData\CustomerSearchQuery;
use App\Services\DberpPermissionService;
use App\Tools\Contracts\Tool;
use App\Tools\Enums\ToolRisk;
use App\Tools\Support\ToolListSchema;
use App\Tools\ToolExecutionContext;
use App\Tools\ToolMetadata;
use App\Tools\ToolPermission;

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

    public function metadata(): ToolMetadata
    {
        return new ToolMetadata(
            name: 'customer.search',
            version: 1,
            description: '按编码、名称检索客户档案（只读）。',
            permission: ToolPermission::permission(DberpPermissionService::VIEW_MASTER_CUSTOMER),
            risk: ToolRisk::ReadOnly,
            inputSchema: [
                'type' => 'object',
                'additionalProperties' => false,
                'properties' => [
                    'q' => [
                        'type' => 'string',
                        'minLength' => 1,
                        'maxLength' => 100,
                    ],
                    'is_enabled' => ['type' => 'boolean'],
                    'customer_category_id' => [
                        'type' => 'integer',
                        'minimum' => 1,
                    ],
                    ...ToolListSchema::paginationInputProperties(
                        CustomerSearchQuery::SORTABLE,
                        CustomerSearchQuery::DEFAULT_SORT,
                    ),
                ],
            ],
            outputSchema: ToolListSchema::paginatedOutputSchema(PartnerToolSchema::customerGetOutput()),
            owner: 'core',
        );
    }

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