<?php

declare(strict_types=1);

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

use App\Http\Controllers\API\V1\Pouzivatel\Notifications\Transformers\NotificationTransformer;
use Dingo\Api\Http\Response;
use Dingo\Api\Routing\Helpers;
use App\Http\Controllers\API\BaseController;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

/**
 * Class ListAction
 *
 * @package App\Http\Controllers\API\V1\Pouzivatel\Notifications\Frontend
 */
final class ListAction extends BaseController
{
    use Helpers;

    /**
     * @return Response
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function __invoke(): Response
    {
        $page = request()->get('page', 1);
        $limit = ($limit = (int) request()->get('limit', 15)) > 50 ? 50 : $limit;
        $stav = request()->get('stav', null);

        $pouzivatel = $this->auth->user();

        switch ($stav) {
            case 'precitane':
                $notifikacie = $pouzivatel->readNotifications();
                break;
            case 'neprecitane':
                $notifikacie = $pouzivatel->unreadNotifications();
                break;
            default:
                $notifikacie = $pouzivatel->notifications();
        }

        $notifications = $notifikacie->paginate($limit, ['*'], 'page', $page);

        if ($notifications->isEmpty()) {
            return $this->response->noContent();
        }

        return $this->response->paginator($notifications, NotificationTransformer::class);
    }
}
