<?php

declare(strict_types=1);

namespace ApiModel\Route;

use ApiModel\Action\SecurityAction;
use ApiModel\Shared\Support\HttpRunner;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Routing\RouteCollectorProxy;

final class SecurityRoutes
{
    public static function register(RouteCollectorProxy $g, SecurityAction $securityAction, string $uuid): void
    {
        // 1. Menú dinámico del usuario
        $g->get('/auth/menu', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->getMenu($request)));

        // 2. Gestión de Roles
        $g->get('/security/roles', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->listRoles($request)));
        $g->get("/security/roles/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->getRole($request, $args['id'])));
        $g->post('/security/roles', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->createRole($request)));
        $g->patch("/security/roles/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->updateRole($request, $args['id'])));
        $g->delete("/security/roles/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->deleteRole($request, $args['id'])));

        // 3. Asignación de Permisos en Perfiles
        $g->get("/security/roles/{id:$uuid}/permissions", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->getRolePermissions($request, $args['id'])));
        $g->post("/security/roles/{id:$uuid}/permissions", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->saveRolePermission($request, $args['id'])));

        // 4. Módulos del menú
        $g->get('/security/modules', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->listModules($request)));
        $g->post('/security/modules', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->createModule($request)));
        $g->patch("/security/modules/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->updateModule($request, $args['id'])));
        $g->delete("/security/modules/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->deleteModule($request, $args['id'])));

        // 5. Páginas del menú
        $g->get('/security/pages', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->listPages($request)));
        $g->post('/security/pages', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->createPage($request)));
        $g->patch("/security/pages/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->updatePage($request, $args['id'])));
        $g->delete("/security/pages/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->deletePage($request, $args['id'])));

        // 6. Plantillas de correo (Messages)
        $g->get('/security/messages', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->listMessages($request)));
        $g->get('/security/messages/{code}', fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->getMessage($request, $args['code'])));
        $g->post('/security/messages', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->createMessage($request)));
        $g->patch("/security/messages/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->updateMessage($request, $args['id'])));

        // 7. Gestión de Usuarios
        $g->get('/security/users', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->listUsers($request)));
        $g->post('/security/users', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->createUser($request)));
        $g->patch("/security/users/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->updateUser($request, $args['id'])));
        $g->delete("/security/users/{id:$uuid}", fn (ServerRequestInterface $request, ResponseInterface $response, array $args) => HttpRunner::runJson($request, $response, fn () => $securityAction->deleteUser($request, $args['id'])));

        // 8. Visor de Logs
        $g->get('/security/logs', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->listLogsSummary($request)));
        $g->post('/security/logs/detail', fn (ServerRequestInterface $request, ResponseInterface $response) => HttpRunner::runJson($request, $response, fn () => $securityAction->getUserLogsDetail($request)));
    }
}
