<?php

declare(strict_types=1);

use ServerManager\Core\App;
use ServerManager\Controllers\AuthController;
use ServerManager\Controllers\DashboardController;
use ServerManager\Controllers\ServerController;
use ServerManager\Controllers\TerminalController;
use ServerManager\Controllers\AdminController;
use ServerManager\Controllers\ApiController;
use ServerManager\Controllers\TeamController;
use ServerManager\Middleware\AuthMiddleware;
use ServerManager\Middleware\CSRFMiddleware;
use ServerManager\Middleware\RateLimitMiddleware;
use ServerManager\Middleware\RoleMiddleware;

$router = App::getInstance()->getRouter();

$router->get('/', [DashboardController::class, 'index'], [AuthMiddleware::class]);
$router->get('/dashboard', [DashboardController::class, 'index'], [AuthMiddleware::class]);
$router->get('/dashboard/stats', [DashboardController::class, 'stats'], [AuthMiddleware::class]);
$router->get('/dashboard/refresh-metrics', [DashboardController::class, 'refreshMetrics'], [AuthMiddleware::class]);

$router->get('/login', [AuthController::class, 'loginForm']);
$router->post('/login', [AuthController::class, 'login'], [CSRFMiddleware::class, RateLimitMiddleware::class]);
$router->get('/register', [AuthController::class, 'registerForm']);
$router->post('/register', [AuthController::class, 'register'], [CSRFMiddleware::class]);
$router->get('/logout', [AuthController::class, 'logout']);

$router->get('/profile', [AuthController::class, 'profile'], [AuthMiddleware::class]);
$router->post('/profile', [AuthController::class, 'updateProfile'], [AuthMiddleware::class, CSRFMiddleware::class]);

$router->get('/servers', [ServerController::class, 'index'], [AuthMiddleware::class]);
$router->get('/servers/create', [ServerController::class, 'create'], [AuthMiddleware::class, RoleMiddleware::class]);
$router->post('/servers/create', [ServerController::class, 'store'], [AuthMiddleware::class, CSRFMiddleware::class, RoleMiddleware::class]);
$router->get('/servers/:id', [ServerController::class, 'show'], [AuthMiddleware::class]);
$router->get('/servers/:id/edit', [ServerController::class, 'edit'], [AuthMiddleware::class]);
$router->post('/servers/:id/edit', [ServerController::class, 'update'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/delete', [ServerController::class, 'delete'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/toggle', [ServerController::class, 'toggleActive'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/test-connection', [ServerController::class, 'testConnection'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/reboot', [ServerController::class, 'reboot'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/shutdown', [ServerController::class, 'shutdown'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/restart-service', [ServerController::class, 'restartService'], [AuthMiddleware::class, CSRFMiddleware::class]);

$router->get('/servers/:id/processes', [ServerController::class, 'processes'], [AuthMiddleware::class]);
$router->post('/servers/:id/processes/kill', [ServerController::class, 'killProcess'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->get('/servers/:id/system-info', [ServerController::class, 'systemInfo'], [AuthMiddleware::class]);
$router->get('/servers/:id/nginx', [ServerController::class, 'nginx'], [AuthMiddleware::class]);
$router->get('/servers/:id/nginx/file', [ServerController::class, 'nginxGetFile'], [AuthMiddleware::class]);
$router->post('/servers/:id/nginx/file', [ServerController::class, 'nginxSaveFile'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/nginx/toggle-site', [ServerController::class, 'nginxToggleSite'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/nginx/site', [ServerController::class, 'nginxCreateSite'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->delete('/servers/:id/nginx/site', [ServerController::class, 'nginxDeleteSite'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->get('/servers/:id/ssl', [ServerController::class, 'ssl'], [AuthMiddleware::class]);
$router->post('/servers/:id/ssl', [ServerController::class, 'sslAction'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->get('/servers/:id/users', [ServerController::class, 'systemUsers'], [AuthMiddleware::class]);
$router->post('/servers/:id/users', [ServerController::class, 'systemUsersAction'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->get('/servers/:id/services', [ServerController::class, 'services'], [AuthMiddleware::class]);
$router->post('/servers/:id/services', [ServerController::class, 'servicesAction'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->get('/servers/:id/database', [ServerController::class, 'database'], [AuthMiddleware::class]);
$router->post('/servers/:id/database', [ServerController::class, 'databaseAction'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/nginx', [ServerController::class, 'nginxAction'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->get('/servers/:id/logs', [ServerController::class, 'logs'], [AuthMiddleware::class]);
$router->get('/servers/:id/metrics', [ServerController::class, 'metrics'], [AuthMiddleware::class]);
$router->get('/sidebar/servers', [ServerController::class, 'sidebarServers'], [AuthMiddleware::class]);

$router->get('/teams', [TeamController::class, 'index'], [AuthMiddleware::class, RoleMiddleware::class]);
$router->get('/teams/search-users', [TeamController::class, 'searchUsers'], [AuthMiddleware::class, RoleMiddleware::class]);
$router->get('/teams/create', [TeamController::class, 'create'], [AuthMiddleware::class, RoleMiddleware::class]);
$router->post('/teams/create', [TeamController::class, 'store'], [AuthMiddleware::class, CSRFMiddleware::class, RoleMiddleware::class]);
$router->get('/teams/:id', [TeamController::class, 'show'], [AuthMiddleware::class, RoleMiddleware::class]);
$router->post('/teams/:id/update', [TeamController::class, 'update'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/teams/:id/delete', [TeamController::class, 'delete'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/teams/:id/add-user', [TeamController::class, 'addUser'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/teams/:id/remove-user', [TeamController::class, 'removeUser'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/teams/:id/add-server', [TeamController::class, 'addServer'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/teams/:id/remove-server', [TeamController::class, 'removeServer'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/teams/:id/update-server-role', [TeamController::class, 'updateServerRole'], [AuthMiddleware::class, CSRFMiddleware::class]);

$router->get('/terminal/:id', [TerminalController::class, 'index'], [AuthMiddleware::class]);
$router->post('/terminal/:id/execute', [TerminalController::class, 'execute'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->get('/terminal/:id/history', [TerminalController::class, 'history'], [AuthMiddleware::class]);
$router->post('/terminal/:id/clear-history', [TerminalController::class, 'clearHistory'], [AuthMiddleware::class, CSRFMiddleware::class]);

$router->group(['prefix' => 'admin', 'middleware' => [AuthMiddleware::class, RoleMiddleware::class]], function (ServerManager\Core\Router $router) {
    $router->get('/users', [AdminController::class, 'users']);
    $router->post('/users/create', [AdminController::class, 'createUser'], [CSRFMiddleware::class]);
    $router->post('/users/:id/update', [AdminController::class, 'updateUser'], [CSRFMiddleware::class]);
    $router->post('/users/:id/delete', [AdminController::class, 'deleteUser'], [CSRFMiddleware::class]);

    $router->get('/audit', [AdminController::class, 'auditLogs']);
    $router->get('/security', [AdminController::class, 'securitySettings']);
    $router->get('/app-version', [AdminController::class, 'appVersion']);
    $router->post('/app-version', [AdminController::class, 'appVersion'], [CSRFMiddleware::class]);
    $router->get('/notifications', [AdminController::class, 'notifications']);
    $router->post('/notifications/send', [AdminController::class, 'sendNotification'], [CSRFMiddleware::class]);
});

$router->get('/api/status', [ApiController::class, 'status']);
$router->get('/api/servers', [ApiController::class, 'servers']);
$router->post('/api/servers', [ApiController::class, 'serverAdd']);
$router->get('/api/servers/:id', [ApiController::class, 'serverDetail']);
$router->put('/api/servers/:id', [ApiController::class, 'serverUpdate']);
$router->delete('/api/servers/:id', [ApiController::class, 'serverDelete']);
$router->get('/api/servers/:id/metrics', [ApiController::class, 'serverMetrics']);
$router->post('/api/servers/:id/execute', [ApiController::class, 'executeCommand']);
$router->post('/api/auth/login', [ApiController::class, 'login']);
$router->post('/api/auth/register', [ApiController::class, 'register']);
$router->get('/api/app-version', [ApiController::class, 'appVersion']);
$router->get('/api/servers/:id/services', [ApiController::class, 'serverServices']);
$router->post('/api/servers/:id/reboot', [ApiController::class, 'serverReboot']);
$router->post('/api/servers/:id/shutdown', [ApiController::class, 'serverShutdown']);
$router->post('/api/servers/:id/test-connection', [ApiController::class, 'serverTestConnection']);
$router->post('/api/servers/:id/service-action', [ApiController::class, 'serverServiceAction']);
$router->get('/api/notifications', [ApiController::class, 'notifications']);
$router->get('/api/notifications/unread-count', [ApiController::class, 'unreadCount']);
$router->post('/api/notifications/:id/read', [ApiController::class, 'markNotificationRead']);
$router->post('/api/notifications/read-all', [ApiController::class, 'markAllNotificationsRead']);
$router->get('/api/users', [ApiController::class, 'users']);
$router->get('/api/profile', [ApiController::class, 'profile']);
$router->put('/api/profile', [ApiController::class, 'updateProfile']);
$router->get('/api/refresh-metrics', [ApiController::class, 'refreshAllMetrics']);
$router->post('/api/metrics/push', [ApiController::class, 'pushMetrics']);

$router->post('/servers/:id/agent/install', [ServerController::class, 'agentInstall'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/agent/uninstall', [ServerController::class, 'agentUninstall'], [AuthMiddleware::class, CSRFMiddleware::class]);
$router->post('/servers/:id/agent/regenerate-key', [ServerController::class, 'agentRegenerateKey'], [AuthMiddleware::class, CSRFMiddleware::class]);
