<?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\InventoryBusinessType;
use App\Business\Inventory\Enums\InventoryChangeDirection;
use App\Business\Inventory\InventoryTransactionReadQuery;
use App\Tools\Contracts\Tool;
use App\Tools\Entity\EntityReferenceSchema;
use App\Tools\Enums\ToolRisk;
use App\Tools\Support\ToolDateSchema;
use App\Tools\Support\ToolListSchema;
use App\Tools\ToolExecutionContext;
use App\Tools\ToolMetadata;
use App\Tools\ToolPermission;

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

    public function metadata(): ToolMetadata
    {
        $businessTypes = array_map(
            static fn (InventoryBusinessType $type): string => $type->value,
            InventoryBusinessType::cases(),
        );
        $directions = array_map(
            static fn (InventoryChangeDirection $direction): string => $direction->value,
            InventoryChangeDirection::cases(),
        );

        $itemSchema = [
            'type' => 'object',
            'required' => [
                'id',
                'product_id',
                'product_code',
                'product_name',
                'warehouse_id',
                'warehouse_name',
                'direction',
                'business_type',
                'change_quantity',
                'before_quantity',
                'after_quantity',
            ],
            '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_name' => ['type' => 'string'],
                'direction' => ['type' => 'string'],
                'business_type' => ['type' => 'string'],
                'business_number' => ['type' => 'string'],
                'change_quantity' => ['type' => 'string'],
                'before_quantity' => ['type' => 'string'],
                'after_quantity' => ['type' => 'string'],
                'business_time' => ['type' => 'string'],
                'entity_refs' => [
                    'type' => 'array',
                    'items' => EntityReferenceSchema::object(),
                ],
            ],
        ];

        return new ToolMetadata(
            name: 'inventory.transactions',
            version: 1,
            description: '查询库存流水（只读），过滤口径与查询中心库存流水一致。',
            permission: ToolPermission::report('inventory-transaction-query'),
            risk: ToolRisk::ReadOnly,
            inputSchema: [
                'type' => 'object',
                'additionalProperties' => false,
                'properties' => [
                    'product_id' => ['type' => 'integer', 'minimum' => 1],
                    'product_sku_id' => ['type' => 'integer', 'minimum' => 1],
                    'warehouse_id' => ['type' => 'integer', 'minimum' => 1],
                    'business_type' => [
                        'type' => 'string',
                        'enum' => $businessTypes,
                    ],
                    'direction' => [
                        'type' => 'string',
                        'enum' => $directions,
                    ],
                    'date_from' => ToolDateSchema::dateProperty('Inclusive ledger calendar date Y-m-d.'),
                    'date_until' => ToolDateSchema::dateProperty('Inclusive ledger calendar date Y-m-d.'),
                    'period_preset' => ToolDateSchema::periodPresetProperty(),
                    ...ToolListSchema::paginationInputProperties(
                        InventoryTransactionReadQuery::SORTABLE,
                        InventoryTransactionReadQuery::DEFAULT_SORT,
                    ),
                ],
            ],
            outputSchema: ToolListSchema::paginatedOutputSchema($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);
    }
}
