<?php
use App\Core\Router;
use App\Controllers\AuthController;
use App\Controllers\PostController;
use App\Controllers\UserController;
use App\Controllers\AdminController;
use App\Controllers\MessageController;
use App\Controllers\ApiController;
use App\Controllers\LangController;
use App\Controllers\DashboardController;
use App\Controllers\SearchController;
use App\Controllers\NotificationController;
use App\Controllers\ForumController;
use App\Controllers\SettingsController;
use App\Middleware\AuthMiddleware;
use App\Middleware\AdminMiddleware;
use App\Middleware\CsrfMiddleware;
use App\Middleware\RateLimitMiddleware;

$router = new Router();

// Global Middlewares (handled in index.php or here)
$common = [RateLimitMiddleware::class, CsrfMiddleware::class];

// Dashboard
$router->get('/dashboard', [DashboardController::class, 'index'], [AuthMiddleware::class]);
$router->get('/api/quick-stats', [DashboardController::class, 'getQuickStats'], [AuthMiddleware::class]);

// Auth
$router->get('/language', [LangController::class, 'set']);
$router->post('/set-lang', [LangController::class, 'setAjax'], [RateLimitMiddleware::class]);
$router->get('/login', [AuthController::class, 'showLogin']);
$router->post('/login', [AuthController::class, 'login'], $common);
$router->get('/register', [AuthController::class, 'showRegister']);
$router->post('/register', [AuthController::class, 'register'], $common);
$router->get('/logout', [AuthController::class, 'logout']);

// Posts
$router->get('/', [PostController::class, 'index']);
$router->get('/post/create', [PostController::class, 'showCreateForm'], [AuthMiddleware::class]);
$router->get('/post/{slug}', [PostController::class, 'show']);
$router->get('/post/{slug}/edit', [PostController::class, 'showEditForm'], [AuthMiddleware::class]);
$router->post('/post/update/{id}', [PostController::class, 'update'], array_merge($common, [AuthMiddleware::class]));
$router->post('/post/store', [PostController::class, 'store'], array_merge($common, [AuthMiddleware::class]));
$router->post('/post/vote', [PostController::class, 'vote'], array_merge($common, [AuthMiddleware::class]));
$router->post('/comment/store', [PostController::class, 'storeComment'], array_merge($common, [AuthMiddleware::class]));
$router->post('/comment/accept', [PostController::class, 'acceptComment'], array_merge($common, [AuthMiddleware::class]));
$router->get('/c/{slug}', [PostController::class, 'category']);

// User
$router->get('/users', [UserController::class, 'index']);
$router->get('/u/{username}', [UserController::class, 'profile']);
$router->get('/profile/edit', [UserController::class, 'edit'], [AuthMiddleware::class]);
$router->post('/profile/update', [UserController::class, 'update'], array_merge($common, [AuthMiddleware::class]));
$router->get('/notifications', [UserController::class, 'notifications'], [AuthMiddleware::class]);
$router->get('/messages', [MessageController::class, 'index'], [AuthMiddleware::class]);
$router->get('/messages/{id}', [MessageController::class, 'show'], [AuthMiddleware::class]);
$router->post('/messages/delete/{id}', [MessageController::class, 'delete'], array_merge($common, [AuthMiddleware::class]));
$router->post('/messages/store', [MessageController::class, 'store'], array_merge($common, [AuthMiddleware::class]));

// Admin Actions
$router->post('/admin/posts/delete/{id}', [AdminController::class, 'deletePost'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->post('/admin/posts/block/{id}', [AdminController::class, 'blockPost'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->post('/admin/posts/pin/{id}', [AdminController::class, 'pinTopic'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->post('/admin/comments/delete/{id}', [AdminController::class, 'deleteComment'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->post('/admin/users/ban/{id}', [AdminController::class, 'banUser'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->post('/admin/users/unban/{id}', [AdminController::class, 'unbanUser'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));

// Admin Settings & Pages
$router->post('/admin/settings', [AdminController::class, 'saveSettings'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->get('/admin/pages', [AdminController::class, 'pages'], [AuthMiddleware::class, AdminMiddleware::class]);
$router->get('/admin/pages/create', [AdminController::class, 'createPage'], [AuthMiddleware::class, AdminMiddleware::class]);
$router->post('/admin/pages/store', [AdminController::class, 'storePage'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->get('/admin/pages/edit/{id}', [AdminController::class, 'editPage'], [AuthMiddleware::class, AdminMiddleware::class]);
$router->post('/admin/pages/update/{id}', [AdminController::class, 'updatePage'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->post('/admin/pages/delete/{id}', [AdminController::class, 'deletePage'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));

// Public Pages
$router->get('/p/{slug}', [SettingsController::class, 'showPage']);

// Search
$router->get('/search', [\App\Controllers\SearchController::class, 'search']);

// Notifications
$router->get('/notifications', [NotificationController::class, 'index'], [AuthMiddleware::class]);
$router->get('/api/notifications/unread', [NotificationController::class, 'getUnread'], [AuthMiddleware::class]);
$router->post('/api/notifications/{id}/read', [NotificationController::class, 'markAsRead'], array_merge($common, [AuthMiddleware::class]));
// clear all / mark all as read
$router->post('/api/notifications/mark-all-read', [NotificationController::class, 'markAllRead'], array_merge($common, [AuthMiddleware::class]));
$router->post('/api/notifications/clear-all', [NotificationController::class, 'clearAll'], array_merge($common, [AuthMiddleware::class]));

// Forum
$router->get('/forums', [ForumController::class, 'index']);
$router->get('/forum/{slug}', [ForumController::class, 'show']);
$router->get('/forums/trending', [ForumController::class, 'trending']);
$router->get('/forums/stats', [ForumController::class, 'stats']);
$router->get('/forums/search', [ForumController::class, 'search']);
$router->get('/forum/moderation', [ForumController::class, 'moderationDash'], [AuthMiddleware::class, AdminMiddleware::class]);

// Settings
$router->get('/settings', [SettingsController::class, 'index'], [AuthMiddleware::class]);
$router->get('/api/preferences', [SettingsController::class, 'getPreferences'], [AuthMiddleware::class]);
$router->post('/api/settings/theme', [SettingsController::class, 'saveTheme'], array_merge($common, [AuthMiddleware::class]));
$router->post('/api/settings/notifications', [SettingsController::class, 'saveNotificationPreferences'], array_merge($common, [AuthMiddleware::class]));
$router->post('/api/settings/shortcuts', [SettingsController::class, 'saveKeyboardShortcuts'], array_merge($common, [AuthMiddleware::class]));
$router->post('/api/settings/accessibility', [SettingsController::class, 'saveAccessibility'], array_merge($common, [AuthMiddleware::class]));
$router->get('/privacy', [SettingsController::class, 'privacy'], [AuthMiddleware::class]);
$router->post('/api/settings/privacy', [SettingsController::class, 'savePrivacy'], array_merge($common, [AuthMiddleware::class]));

// Admin
$router->get('/admin', [AdminController::class, 'dashboard'], [AuthMiddleware::class, AdminMiddleware::class]);
$router->get('/admin/users', [AdminController::class, 'users'], [AuthMiddleware::class, AdminMiddleware::class]);
$router->get('/admin/settings', [AdminController::class, 'settings'], [AuthMiddleware::class, AdminMiddleware::class]);
$router->get('/admin/categories', [AdminController::class, 'categories'], [AuthMiddleware::class, AdminMiddleware::class]);
$router->post('/admin/categories/store', [AdminController::class, 'storeCategory'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->post('/admin/categories/update/{id}', [AdminController::class, 'updateCategory'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->post('/admin/categories/delete/{id}', [AdminController::class, 'deleteCategory'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));
$router->post('/api/admin/posts/reorder', [AdminController::class, 'updatePostsOrder'], array_merge($common, [AuthMiddleware::class, AdminMiddleware::class]));

// API
$router->get('/api/posts', [\App\Controllers\ApiController::class, 'getPosts']);
$router->get('/api/posts/{slug}', [\App\Controllers\ApiController::class, 'getPost']);
$router->get('/api/users/{username}', [\App\Controllers\ApiController::class, 'getUser']);

return $router;
