<?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;

/**
 * Class MarkAsUnreadAction
 *
 * @package App\Http\Controllers\API\V1\Pouzivatel\Notifications\Frontend
 */
final class MarkAsUnreadAction extends BaseController
{
    /**
     * @param Uuid $uuid
     */
    public function __invoke(UuidInterface $uuid): Response
    {
        $pouzivatel = $this->auth->user();

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

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

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