<?php

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

use App\Http\Controllers\API\BaseController;
use App\Notifications\Push\MAAISEKUserNotificationsCountPushNotification;
use Dingo\Api\Http\Response;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Notifications\DatabaseNotification;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

/**
 *
 */
final class GetAction extends BaseController
{
    /**
     * @param Uuid $uuid
     * @return Response
     */
    public function __invoke(UuidInterface $uuid): Response
    {
        $pouzivatel = $this->auth->user();

        try {
            /** @var DatabaseNotification $notification */
            $notification = $pouzivatel->notifications()->where('id', $uuid)->firstOrFail();

            if ($notification->unread()) {
                $notification->markAsRead();
                $pouzivatel->notify(new MAAISEKUserNotificationsCountPushNotification());
            }
        } catch (ModelNotFoundException $e) {
            return $this->response->errorNotFound();
        }

        return $this->response->item($notification);
    }
}
