<?php

declare(strict_types=1);

use App\Application\Actions\User\ListUsersAction;
use App\Application\Actions\User\ViewUserAction;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\App;
use Slim\Interfaces\RouteCollectorProxyInterface as Group;

return function (App $app) {
    $app->options('/{routes:.*}', function (Request $request, Response $response) {
        // CORS Pre-Flight OPTIONS Request Handler
        return $response;
    });

    $app->get('/', \App\Application\Actions\IndexAction::class);

    $app->group('/users', function (Group $group) {
        $group->get('', ListUsersAction::class);
        $group->get('/{id}', ViewUserAction::class);
    });

    $app->get('/content', \App\Application\Actions\Content\AllContentsAction::class);
    $app->get('/content/{key}', \App\Application\Actions\Content\ContentAction::class);

    $app->get('/blog', \App\Application\Actions\BlogIndexAction::class);
    $app->get('/blog/{stub}', \App\Application\Actions\BlogPostAction::class);

    // Admin login/logout routes
    $app->map(['GET', 'POST'], '/admin/login', \App\Application\Actions\Admin\AdminController::class . ':login');
    $app->get('/admin/logout', \App\Application\Actions\Admin\AdminController::class . ':logout');

    // Protected admin area
    $app->group('/admin', function (Group $group) {
        $group->get('', \App\Application\Actions\Admin\DashboardAction::class);
    $group->get('/blog-visits', \App\Application\Actions\Admin\BlogVisitsAction::class);
    $group->get('/contents', \App\Application\Actions\Content\ContentCrudAction::class . ':list');
    $group->get('/access-logs', \App\Application\Actions\Admin\AccessLogsAction::class);
        $group->map(['GET', 'POST'], '/contents/create', \App\Application\Actions\Content\ContentCrudAction::class . ':create');
        $group->map(['GET', 'POST'], '/contents/edit/{id}', \App\Application\Actions\Content\ContentCrudAction::class . ':edit');
        $group->get('/contents/delete/{id}', \App\Application\Actions\Content\ContentCrudAction::class . ':delete');
    })->add(\App\Application\Middleware\AdminAuthMiddleware::class);
};
