<?php

use CodeIgniter\Router\RouteCollection;
use App\Core\Modules\Services\ModuleManager;

/** @var RouteCollection $routes */
$routes->get('/', static fn () => redirect()->to('/dashboard'));

$routes->get('login', '\App\Core\Auth\Controllers\AuthController::login');
$routes->post('login', '\App\Core\Auth\Controllers\AuthController::attempt');
$routes->get('logout', '\App\Core\Auth\Controllers\AuthController::logout', ['filter' => 'auth']);

$routes->group('', ['filter' => 'auth'], static function (RouteCollection $routes) {
    $routes->get('dashboard', '\App\Core\Dashboard\Controllers\DashboardController::index');

    $routes->get('users', '\App\Core\Users\Controllers\UserController::index', ['filter' => 'permission:core.users.manage']);
    $routes->post('users', '\App\Core\Users\Controllers\UserController::store', ['filter' => 'permission:core.users.manage']);

    $routes->get('roles', '\App\Core\Roles\Controllers\RoleController::index', ['filter' => 'permission:core.roles.manage']);
    $routes->post('roles/(:num)/permissions', '\App\Core\Roles\Controllers\RoleController::updatePermissions/$1', ['filter' => 'permission:core.roles.manage']);

    $routes->get('modules', '\App\Core\Modules\Controllers\ModuleController::index', ['filter' => 'permission:core.modules.manage']);
    $routes->post('modules/(:segment)/toggle', '\App\Core\Modules\Controllers\ModuleController::toggle/$1', ['filter' => 'permission:core.modules.manage']);

    $routes->get('settings', '\App\Core\Settings\Controllers\SettingsController::index', ['filter' => 'permission:core.settings.manage']);
    $routes->post('settings', '\App\Core\Settings\Controllers\SettingsController::save', ['filter' => 'permission:core.settings.manage']);

    $routes->get('database-connections', '\App\Core\Database\Controllers\DatabaseConnectionController::index', ['filter' => 'permission:core.database.manage']);
    $routes->post('database-connections', '\App\Core\Database\Controllers\DatabaseConnectionController::store', ['filter' => 'permission:core.database.manage']);
    $routes->get('database-connections/(:num)', '\App\Core\Database\Controllers\DatabaseConnectionController::show/$1', ['filter' => 'permission:core.database.manage']);

    $routes->get('ai-history', '\App\Core\Ai\Controllers\AiHistoryController::index', ['filter' => 'permission:core.ai.use']);
    $routes->get('audit', '\App\Core\Audit\Controllers\AuditController::index', ['filter' => 'permission:core.audit.view']);
});

(new ModuleManager())->registerRoutes($routes);
