<?php

declare(strict_types=1);

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

namespace App\Tools\Inventory;

use App\Business\Inventory\Enums\InventoryBalanceStockStatus;
use App\Business\Inventory\InventoryBalanceSearchQuery;
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 InventoryBalanceSearchTool implements Tool
{
    public function __construct(
        private readonly InventoryBalanceSearchQuery $query,
    ) {}

    public function metadata(): ToolMetadata
    {
        return new ToolMetadata(
            name: 'inventory.balance.search',
            version: 1,
            description: '按商品、SKU、仓库、分类、关键词与结存数量检索库存余额（只读，分页）。',
            permission: ToolPermission::permission(DberpPermissionService::VIEW_INVENTORY_BALANCE),
            risk: ToolRisk::ReadOnly,
            inputSchema: [
                'type' => 'object',
                'additionalProperties' => false,
                'properties' => [
                    'q' => [
                        'type' => 'string',
                        'minLength' => 1,
                        'maxLength' => 100,
                    ],
                    'product_id' => ['type' => 'integer', 'minimum' => 1],
                    'product_sku_id' => ['type' => 'integer', 'minimum' => 1],
                    'warehouse_id' => ['type' => 'integer', 'minimum' => 1],
                    'category_id' => ['type' => 'integer', 'minimum' => 1],
                    'stock_status' => [
                        'type' => 'string',
                        'enum' => InventoryBalanceStockStatus::values(),
                    ],
                    'quantity_min' => [
                        'type' => 'number',
                        'minimum' => 0,
                    ],
                    'quantity_max' => [
                        'type' => 'number',
                        'minimum' => 0,
                    ],
                    ...ToolListSchema::paginationInputProperties(
                        InventoryBalanceSearchQuery::SORTABLE,
                        InventoryBalanceSearchQuery::DEFAULT_SORT,
                    ),
                ],
            ],
            outputSchema: ToolListSchema::paginatedOutputSchema(EntityReferenceSchema::withRefs($this->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);
    }

    /**
     * @return array<string, mixed>
     */
    private function itemSchema(): array
    {
        return [
            'type' => 'object',
            'required' => [
                'id',
                'product_id',
                'product_code',
                'product_name',
                'warehouse_id',
                'warehouse_name',
                'realtime_quantity',
                'available_quantity',
                'occupied_quantity',
                'safety_stock',
            ],
            'additionalProperties' => false,
            'properties' => [
                'id' => ['type' => 'integer'],
                'product_id' => ['type' => 'integer'],
                'product_code' => ['type' => 'string'],
                'product_name' => ['type' => 'string'],
                'product_sku_id' => ['type' => 'integer'],
                'sku_code' => ['type' => 'string'],
                'spec_text' => ['type' => 'string'],
                'warehouse_id' => ['type' => 'integer'],
                'warehouse_code' => ['type' => 'string'],
                'warehouse_name' => ['type' => 'string'],
                'category_id' => ['type' => 'integer'],
                'category_name' => ['type' => 'string'],
                'realtime_quantity' => ['type' => 'string'],
                'available_quantity' => ['type' => 'string'],
                'occupied_quantity' => ['type' => 'string'],
                'safety_stock' => ['type' => 'string'],
                'last_moved_at' => ['type' => 'string'],
            ],
        ];
    }
}
