<?php

declare(strict_types=1);

namespace App\Services;

use Illuminate\Http\Request;

/**
 * Authorizes launching the standalone updater for johneady/installer-toolkit.
 *
 * Registered as `updates.authorize_upload` and resolved by the toolkit's
 * LaunchUpdaterController via app()->call(). It must be a string-based
 * callable (an invokable class), never a Closure: the optimize step runs
 * `config:cache`, which cannot serialize Closures.
 *
 * Access is granted only to users who can access the `server` Filament panel.
 * User::canAccessPanel() requires is_admin, so launching the updater is
 * restricted to administrators (consistent with the panel middleware).
 */
class UpdateUploadAuthorization
{
    public function __invoke(Request $request): bool
    {
        $user = $request->user();

        if ($user === null) {
            return false;
        }

        return $user->canAccessPanel(filament()->getPanel('server'));
    }
}
