<?php

declare(strict_types=1);

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

namespace App\Support\PluginIntegration;

use Illuminate\Http\Request;

/**
 * 宿主侧 OAuth 同意桥。插件未安装时不得改写 Passport 批准/拒绝。
 */
final class DberpMcpOAuthConsentBridge
{
    private const GRANT_SERVICE = 'Loongdom\DberpMcp\OAuth\DberpMcpOAuthConnectionGrantService';

    /**
     * @param  array<string, mixed>  $parameters
     * @return array<string, mixed>
     */
    public static function presentAuthorization(array $parameters): array
    {
        return OptionalPluginClassDelegate::call(
            self::GRANT_SERVICE,
            static fn (string $serviceClass): array => app($serviceClass)->presentAuthorization($parameters),
            $parameters,
        );
    }

    public static function assertApprovalAllowed(Request $request): void
    {
        OptionalPluginClassDelegate::call(
            self::GRANT_SERVICE,
            static function (string $serviceClass) use ($request): mixed {
                app($serviceClass)->assertApprovalAllowed($request);

                return null;
            },
            null,
        );
    }

    public static function persistApprovedGrant(Request $request): void
    {
        OptionalPluginClassDelegate::call(
            self::GRANT_SERVICE,
            static function (string $serviceClass) use ($request): mixed {
                app($serviceClass)->persistApprovedGrant($request);

                return null;
            },
            null,
        );
    }

    public static function recordDenial(Request $request): void
    {
        OptionalPluginClassDelegate::call(
            self::GRANT_SERVICE,
            static function (string $serviceClass) use ($request): mixed {
                app($serviceClass)->recordDenial($request);

                return null;
            },
            null,
        );
    }
}
