<?php

declare(strict_types=1);

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

namespace App\Providers;

use App\Tools\Contracts\ToolConsumerBoundary;
use App\Tools\Inventory\InventoryBalanceSearchTool;
use App\Tools\Inventory\InventoryGetTool;
use App\Tools\Inventory\InventoryTransactionsTool;
use App\Tools\Inventory\InventoryWarningTool;
use App\Tools\MasterData\CustomerBusinessSummaryTool;
use App\Tools\MasterData\CustomerExposureSearchTool;
use App\Tools\MasterData\CustomerGetTool;
use App\Tools\MasterData\CustomerSearchTool;
use App\Tools\MasterData\SupplierBusinessSummaryTool;
use App\Tools\MasterData\SupplierExposureSearchTool;
use App\Tools\MasterData\SupplierGetTool;
use App\Tools\MasterData\SupplierSearchTool;
use App\Tools\MasterData\WarehouseGetTool;
use App\Tools\MasterData\WarehouseSearchTool;
use App\Tools\PluginToolRegistrar;
use App\Tools\Product\ProductGetTool;
use App\Tools\Product\ProductPerformanceSearchTool;
use App\Tools\Product\ProductSearchTool;
use App\Tools\Purchase\PurchaseOrderGetTool;
use App\Tools\Purchase\PurchaseOrderSearchTool;
use App\Tools\Purchase\PurchaseSummaryTool;
use App\Tools\Purchase\PurchaseTrendCompareTool;
use App\Tools\Sales\SalesOrderGetTool;
use App\Tools\Sales\SalesOrderSearchTool;
use App\Tools\Sales\SalesSummaryTool;
use App\Tools\Sales\SalesTrendCompareTool;
use App\Tools\Settlement\PayableGetTool;
use App\Tools\Settlement\PayableSearchTool;
use App\Tools\Settlement\ReceivableGetTool;
use App\Tools\Settlement\ReceivableSearchTool;
use App\Tools\Settlement\SettlementAgingTool;
use App\Tools\Support\ToolJsonSchemaValidator;
use App\Tools\Support\ToolOutputGuard;
use App\Tools\Support\ToolResultFactory;
use App\Tools\ToolAuthorizer;
use App\Tools\ToolConsumerGateway;
use App\Tools\ToolDiscoveryService;
use App\Tools\ToolExecutor;
use App\Tools\ToolRegistry;
use Illuminate\Support\ServiceProvider;

/**
 * Tool Layer 绑定与 Core Tool 显式注册。禁止目录扫描或自动发现。
 */
final class ToolServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(ToolRegistry::class);
        $this->app->singleton(ToolJsonSchemaValidator::class);
        $this->app->singleton(ToolOutputGuard::class);
        $this->app->singleton(ToolResultFactory::class);
        $this->app->singleton(ToolAuthorizer::class);
        $this->app->singleton(PluginToolRegistrar::class);
        $this->app->singleton(ToolDiscoveryService::class);
        $this->app->singleton(ToolExecutor::class);
        $this->app->singleton(ToolConsumerGateway::class);
        $this->app->bind(ToolConsumerBoundary::class, fn ($app): ToolConsumerGateway => $app->make(ToolConsumerGateway::class));
    }

    public function boot(): void
    {
        // 旧版宿主分批替换时，本 Provider 可能早于 app/Tools 落盘。类未就绪则跳过注册，
        // 让更新轮询能继续 bootstrap；文件到齐后的下一次请求会正常注册 Core Tool。
        if (! class_exists(ToolRegistry::class)) {
            return;
        }

        $registry = $this->app->make(ToolRegistry::class);

        $registry->register($this->app->make(ProductSearchTool::class));
        $registry->register($this->app->make(InventoryGetTool::class));
        $registry->register($this->app->make(InventoryWarningTool::class));
        $registry->register($this->app->make(SalesSummaryTool::class));
        $registry->register($this->app->make(PurchaseSummaryTool::class));
        $registry->register($this->app->make(SalesOrderGetTool::class));
        $registry->register($this->app->make(SalesOrderSearchTool::class));
        $registry->register($this->app->make(ProductGetTool::class));
        $registry->register($this->app->make(CustomerSearchTool::class));
        $registry->register($this->app->make(SupplierSearchTool::class));
        $registry->register($this->app->make(InventoryTransactionsTool::class));
        $registry->register($this->app->make(PurchaseOrderGetTool::class));
        $registry->register($this->app->make(PurchaseOrderSearchTool::class));
        $registry->register($this->app->make(SettlementAgingTool::class));
        $registry->register($this->app->make(WarehouseSearchTool::class));
        $registry->register($this->app->make(WarehouseGetTool::class));
        $registry->register($this->app->make(InventoryBalanceSearchTool::class));
        $registry->register($this->app->make(ReceivableSearchTool::class));
        $registry->register($this->app->make(ReceivableGetTool::class));
        $registry->register($this->app->make(PayableSearchTool::class));
        $registry->register($this->app->make(PayableGetTool::class));
        $registry->register($this->app->make(CustomerGetTool::class));
        $registry->register($this->app->make(CustomerBusinessSummaryTool::class));
        $registry->register($this->app->make(SupplierGetTool::class));
        $registry->register($this->app->make(SupplierBusinessSummaryTool::class));
        $registry->register($this->app->make(ProductPerformanceSearchTool::class));
        $registry->register($this->app->make(CustomerExposureSearchTool::class));
        $registry->register($this->app->make(SupplierExposureSearchTool::class));
        $registry->register($this->app->make(SalesTrendCompareTool::class));
        $registry->register($this->app->make(PurchaseTrendCompareTool::class));
    }
}
