<?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\WarehouseReadQuery;
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 WarehouseGetTool implements Tool
{
    public function __construct(
        private readonly WarehouseReadQuery $query,
    ) {}

    public function metadata(): ToolMetadata
    {
        return new ToolMetadata(
            name: 'warehouse.get',
            version: 1,
            description: '按仓库 id 或编码读取仓库档案详情（只读）。',
            permission: ToolPermission::permission(DberpPermissionService::VIEW_WAREHOUSE),
            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',
                    'warehouse_type',
                    'is_enabled',
                    'is_default',
                    'sort_order',
                ],
                'additionalProperties' => false,
                'properties' => [
                    'id' => ['type' => 'integer'],
                    'code' => ['type' => 'string'],
                    'name' => ['type' => 'string'],
                    'warehouse_type' => ['type' => 'string'],
                    'is_enabled' => ['type' => 'boolean'],
                    'is_default' => ['type' => 'boolean'],
                    'manager_employee_id' => ['type' => 'integer'],
                    'manager_name' => ['type' => 'string'],
                    'sort_order' => ['type' => 'integer'],
                ],
            ]),
            owner: 'core',
        );
    }

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