<?php

declare(strict_types=1);

namespace Builder\Core\Http\Controllers\Dashboard;

use Illuminate\Http\Request;
use Illuminate\Http\Response;

final class HideWidgetController
{
    public function __invoke(Request $request, string $key): Response
    {
        $user = $request->user();

        if ($user !== null && method_exists($user, 'hiddenWidgets') && method_exists($user, 'toggleWidget') && ! in_array($key, $user->hiddenWidgets(), true)) {
            $user->toggleWidget($key);
        }

        return response()->noContent();
    }
}
