<?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
 *
 * @package App\Http\Controllers\API\V1\Pouzivatel\PushNotifications\Frontend
 */
final class StoreUsersPushNotificationTokenAction extends BaseController
{
    /**
     * @param \App\Http\Requests\StoreUserNotificationTokenRequest $request
     * @param \App\Services\PushNotificationsService\PushNotificationsServiceContract $pushNotificationsService
     *
     * @return \Dingo\Api\Http\Response
     */
    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();
    }
}
