<?php
require_once 'includes/helpers.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (function_exists('init_secure_session')) {
    init_secure_session();
}

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit;
}

// Si se accede a index.php sin una URL específica, redirigir al dashboard del rol.
if (!isset($_GET['url']) || empty($_GET['url'])) {
    $dashboard_route = '';
    switch ($_SESSION['role']) {
        case 'administrador': $dashboard_route = 'admin_dashboard'; break;
        case 'empresa':       $dashboard_route = 'company_dashboard'; break;
        case 'alumno':        $dashboard_route = 'student_dashboard'; break;
        case 'profesor':      $dashboard_route = 'teacher_dashboard'; break;
        case 'egresado':      $dashboard_route = 'graduate_dashboard'; break;
        default:
            session_destroy();
            header('Location: login.php?error=session_expired');
            exit;
    }
    header('Location: ' . route_url($dashboard_route));
    exit;
}

require_once 'router.php';
?>