<?php

declare(strict_types=1);

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

namespace App\Tools\Sales;

use App\Business\Sales\SalesOrderReadQuery;
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 SalesOrderGetTool implements Tool
{
    public function __construct(
        private readonly SalesOrderReadQuery $query,
    ) {}

    public function metadata(): ToolMetadata
    {
        $itemSchema = [
            'type' => 'object',
            'required' => [
                'id',
                'product_id',
                'product_code',
                'product_name',
                'quantity',
                'unit_price',
                'line_amount',
                'discount_amount',
                'final_amount',
                'shipped_quantity',
                'returned_quantity',
                'sort_order',
            ],
            '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'],
                'unit_name' => ['type' => 'string'],
                'quantity' => ['type' => 'string'],
                'unit_price' => ['type' => 'string'],
                'line_amount' => ['type' => 'string'],
                'discount_amount' => ['type' => 'string'],
                'final_amount' => ['type' => 'string'],
                'shipped_quantity' => ['type' => 'string'],
                'returned_quantity' => ['type' => 'string'],
                'sort_order' => ['type' => 'integer'],
                'entity_ref' => EntityReferenceSchema::object(),
            ],
        ];

        return new ToolMetadata(
            name: 'sales.order.get',
            version: 1,
            description: '按订单 id 或订单编号读取销售订单详情（只读）。',
            permission: ToolPermission::permission(DberpPermissionService::VIEW_SALES_ORDER),
            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', 'status', 'customer', 'amounts', 'items'],
                'additionalProperties' => false,
                'properties' => [
                    'id' => ['type' => 'integer'],
                    'code' => ['type' => 'string'],
                    'status' => ['type' => 'string'],
                    'business_time' => ['type' => 'string'],
                    'due_date' => ['type' => 'string'],
                    'handler_employee_id' => ['type' => 'integer'],
                    'customer' => EntityReferenceSchema::nestedCounterparty(),
                    'amounts' => [
                        'type' => 'object',
                        'required' => [
                            'total_quantity',
                            'total_amount',
                            'discount_amount',
                            'final_amount',
                            'receivable_amount',
                            'received_amount',
                            'unreceived_amount',
                        ],
                        'additionalProperties' => false,
                        'properties' => [
                            'total_quantity' => ['type' => 'string'],
                            'total_amount' => ['type' => 'string'],
                            'discount_amount' => ['type' => 'string'],
                            'final_amount' => ['type' => 'string'],
                            'receivable_amount' => ['type' => 'string'],
                            'received_amount' => ['type' => 'string'],
                            'unreceived_amount' => ['type' => 'string'],
                        ],
                    ],
                    'items' => [
                        'type' => 'array',
                        'items' => $itemSchema,
                    ],
                ],
            ]),
            owner: 'core',
        );
    }

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