<?php

declare(strict_types=1);

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

namespace App\Support\PluginIntegration;

use Illuminate\Database\Eloquent\Model;

/**
 * 轻量审批插件单据确认守卫的安全桥接（插件未安装时视为未启用）。
 */
final class LightApprovalDocumentGuardBridge
{
    private const GUARD_CLASS = 'Loongdom\DberpLightApproval\Services\ApprovalDocumentGuard';

    public const ADAPTER_PURCHASE_ORDER = 'Loongdom\DberpLightApproval\Adapters\PurchaseOrderApprovalAdapter';

    public const ADAPTER_SALES_ORDER = 'Loongdom\DberpLightApproval\Adapters\SalesOrderApprovalAdapter';

    public const ADAPTER_STOCK_COUNT = 'Loongdom\DberpLightApproval\Adapters\StockCountApprovalAdapter';

    public const ADAPTER_INVENTORY_ADJUSTMENT = 'Loongdom\DberpLightApproval\Adapters\InventoryAdjustmentApprovalAdapter';

    public const ADAPTER_PAYMENT = 'Loongdom\DberpLightApproval\Adapters\PaymentApprovalAdapter';

    public const ADAPTER_RECEIPT = 'Loongdom\DberpLightApproval\Adapters\ReceiptApprovalAdapter';

    public static function isPluginEnabled(): bool
    {
        return OptionalPluginClassDelegate::call(
            self::GUARD_CLASS,
            static fn (string $guardClass): bool => app($guardClass)->isPluginEnabled(),
            false,
        );
    }

    public static function canConfirm(string $adapterClass, Model $document): bool
    {
        return OptionalPluginClassDelegate::call(
            self::GUARD_CLASS,
            static function (string $guardClass) use ($adapterClass, $document): bool {
                $guard = app($guardClass);

                if (! $guard->isPluginEnabled()) {
                    return true;
                }

                if (! class_exists($adapterClass)) {
                    return true;
                }

                return $guard->canConfirm(app($adapterClass), $document);
            },
            true,
        );
    }

    public static function confirmBlockReason(string $adapterClass, Model $document): ?string
    {
        return OptionalPluginClassDelegate::call(
            self::GUARD_CLASS,
            static function (string $guardClass) use ($adapterClass, $document): ?string {
                $guard = app($guardClass);

                if (! $guard->isPluginEnabled() || ! class_exists($adapterClass)) {
                    return null;
                }

                return $guard->confirmBlockReason(app($adapterClass), $document);
            },
            null,
        );
    }

    public static function assertCanConfirmIfEnabled(string $adapterClass, Model $document): void
    {
        OptionalPluginClassDelegate::call(
            self::GUARD_CLASS,
            static function (string $guardClass) use ($adapterClass, $document): void {
                $guard = app($guardClass);

                if (! $guard->isPluginEnabled() || ! class_exists($adapterClass)) {
                    return;
                }

                $guard->assertCanConfirm(app($adapterClass), $document);
            },
            null,
        );
    }

    public static function requiresApproval(string $adapterClass, Model $document): bool
    {
        return OptionalPluginClassDelegate::call(
            self::GUARD_CLASS,
            static function (string $guardClass) use ($adapterClass, $document): bool {
                $guard = app($guardClass);

                if (! $guard->isPluginEnabled() || ! class_exists($adapterClass)) {
                    return false;
                }

                return $guard->requiresApproval(app($adapterClass), $document);
            },
            false,
        );
    }
}
