<?php

declare(strict_types=1);

namespace App\Http\Controllers\API\V1\Pouzivatel\PushNotifications\Frontend;

use App\Http\Controllers\API\BaseController;
use App\Http\Requests\StoreUserNotificationTokenRequest;
use App\Services\PushNotificationsService\PushNotificationsServiceContract;
use Dingo\Api\Http\Response;

/**
 * Class StoreUsersPushNotificationTokenAction
 */
final class StoreUsersPushNotificationTokenAction extends BaseController
{
    public function __invoke(
        StoreUserNotificationTokenRequest $request,
        PushNotificationsServiceContract $pushNotificationsService,
    ): Response {
        /** @var \App\Models\Pouzivatel\Pouzivatel $user */
        $user = $request->user();
        $token = $request->request->get('notification_token');

        $pushNotificationsService->userDeviceTokenStored($user, $token);
        $pushNotificationsService->registerUserToTopics($user);

        return $this->response()->created();
    }
}
