<?php

use CodeIgniter\Router\RouteCollection;

/** @var RouteCollection $routes */

// Root: logged-in users go to their safe landing page; guests to login.
$routes->get('/', static function () {
    $session = session();

    if ($session->get('isLoggedIn')) {
        $roleId = $session->get('role_id') ? (int) $session->get('role_id') : null;

        return redirect()->to((new \Modules\Auth\Services\AuthService())->landingUrlForRole($roleId));
    }

    return redirect()->to('/login');
});

// One-time browser-based installer — see docs/00-xampp-quickstart.md.
// Safe to delete this line (and app/Controllers/Install.php) once you're done.
$routes->get('install', 'Install::index');

$routes->group('', ['filter' => 'auth'], static function ($routes) {
    $routes->get('dashboard', 'Dashboard::index', ['filter' => 'permission:dashboard.view']);
    $routes->get('settings', 'Settings::index', ['filter' => 'admin']);
    $routes->get('settings/company', 'Settings::company', ['filter' => 'admin']);
    $routes->post('settings/company', 'Settings::updateCompany', ['filter' => 'admin']);
    $routes->get('settings/modules', 'Settings::modules', ['filter' => 'admin']);
    $routes->post('settings/modules', 'Settings::updateModules', ['filter' => 'admin']);
    $routes->post('settings/company/logo', 'Settings::uploadCompanyLogo', ['filter' => 'admin']);
    $routes->post('settings/company/logo/remove', 'Settings::removeCompanyLogo', ['filter' => 'admin']);
});

// --- Module route files -------------------------------------------------
// Each module owns its own Config/Routes.php; require them here as
// modules are built out. Keeps this file from growing unbounded.
require APPPATH . 'Modules/Auth/Config/Routes.php';
require APPPATH . 'Modules/Users/Config/Routes.php';
require APPPATH . 'Modules/Roles/Config/Routes.php';
require APPPATH . 'Modules/Departments/Config/Routes.php';
require APPPATH . 'Modules/Employees/Config/Routes.php';
require APPPATH . 'Modules/Leave/Config/Routes.php';
require APPPATH . 'Modules/Attendance/Config/Routes.php';
require APPPATH . 'Modules/Payroll/Config/Routes.php';
require APPPATH . 'Modules/Assets/Config/Routes.php';
require APPPATH . 'Modules/Documents/Config/Routes.php';
require APPPATH . 'Modules/Hrm/Config/Routes.php';
require APPPATH . 'Modules/Scheduling/Config/Routes.php';
require APPPATH . 'Modules/Sale/Config/Routes.php';
require APPPATH . 'Modules/Contracts/Config/Routes.php';
require APPPATH . 'Modules/Messaging/Config/Routes.php';
require APPPATH . 'Modules/Procurement/Config/Routes.php';
require APPPATH . 'Modules/Inventory/Config/Routes.php';
require APPPATH . 'Modules/Accounting/Config/Routes.php';
require APPPATH . 'Modules/Operations/Config/Routes.php';
require APPPATH . 'Modules/Transport/Config/Routes.php';
require APPPATH . 'Modules/Reports/Config/Routes.php';
require APPPATH . 'Modules/Tpin/Config/Routes.php';
