<?php

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

use App\Http\Controllers\API\BaseController;
use App\Http\Controllers\API\V1\Pouzivatel\Notifications\Transformers\NotificationTransformer;
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 MarkAsUnreadAction 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->read()) {
                $notification->markAsUnread();
                $pouzivatel->notify(new MAAISEKUserNotificationsCountPushNotification());
            }
        } catch (ModelNotFoundException $e) {
            return $this->response->errorNotFound();
        }

        return $this->response->item($notification, NotificationTransformer::class);
    }
}
