<?php
/**
 * Custom router for PHP built-in server to handle .htaccess-like URL rewriting
 * Usage: php -S localhost:8000 server-router.php
 */

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = urldecode($uri);

// Serve static files directly
if (preg_match('/\.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$/', $uri)) {
    return false; // Let PHP serve the file
}

// Remove trailing slash
$uri = rtrim($uri, '/');

// Define URL mappings (same as .htaccess rules)
$routes = [
    // Authentication
    '/login' => 'login.php',
    '/logout' => 'logout.php',
    '/register' => 'register.php',
    
    // Admin routes
    '/admin/dashboard' => 'admin/dashboard.php',
    '/admin/jobs' => 'admin/jobs.php',
    '/admin/students' => 'admin/students.php',
    '/admin/graduates' => 'admin/graduates.php',
    '/admin/teachers' => 'admin/teachers.php',
    '/admin/companies' => 'admin/companies.php',
    '/admin/users' => 'admin/users.php',
    '/admin/notifications' => 'admin/notifications.php',
    '/admin/reports' => 'admin/custom-reports.php',
    '/admin/settings' => 'admin/institution-settings.php',
    '/admin/careers' => 'admin/careers.php',
    '/admin/jobs/new' => 'admin/post-job.php',
    '/admin/users/new' => 'admin/add-user.php',
    '/admin/notifications/send' => 'admin/send-notification.php',
    '/admin/auto-update-job-status' => 'admin/auto_update_job_status.php',
    '/admin/export-jobs' => 'admin/export-jobs.php',
    '/admin/upload-students' => 'admin/upload_students.php',
    
    // Company routes
    '/company/dashboard' => 'company/dashboard.php',
    '/company/jobs' => 'company/jobs.php',
    '/company/jobs/new' => 'company/post-job.php',
    '/company/profile' => 'company/profile.php',
    
    // Student routes
    '/student/dashboard' => 'student/dashboard.php',
    '/student/jobs' => 'student/jobs.php',
    '/student/applications' => 'student/applications.php',
    '/student/notifications' => 'student/notifications.php',
    '/student/profile' => 'student/profile.php',
    '/student/resume' => 'student/resume.php',
    '/student/languages' => 'student/languages.php',
    '/student/trainings' => 'student/trainings.php',
    '/student/experience' => 'student/work-experience.php',
    
    // Graduate routes
    '/graduate/dashboard' => 'graduate/dashboard.php',
    '/graduate/jobs' => 'graduate/jobs.php',
    '/graduate/applications' => 'graduate/applications.php',
    '/graduate/notifications' => 'graduate/notifications.php',
    '/graduate/profile' => 'graduate/profile.php',
    '/graduate/resume' => 'graduate/resume.php',
    
    // Teacher routes
    '/teacher/dashboard' => 'teacher/dashboard.php',
    '/teacher/jobs' => 'teacher/jobs.php',
    '/teacher/students' => 'teacher/students.php',
    '/teacher/profile' => 'teacher/profile.php',
];

// Check for exact route match
if (isset($routes[$uri])) {
    $file_path = __DIR__ . '/' . $routes[$uri];
    if (file_exists($file_path)) {
        require_once $file_path;
        return true;
    }
}

// Handle dynamic routes with parameters
$patterns = [
    // Admin dynamic routes
    '~^/admin/jobs/(\d+)$~' => 'admin/view-job.php',
    '~^/admin/job-applications/(\d+)$~' => 'admin/job-applications.php',
    '~^/admin/jobs/(\d+)/edit$~' => 'admin/edit-job.php',    
    '~^/admin/jobs/(\d+)/delete$~' => 'admin/delete-job.php',
    '~^/admin/students/(\d+)$~' => 'admin/view-user.php',
    '~^/admin/students/(\d+)/edit$~' => 'admin/edit-user.php',
    '~^/admin/graduates/(\d+)$~' => 'admin/view-user.php',
    '~^/admin/graduates/(\d+)/edit$~' => 'admin/edit-user.php',
    '~^/admin/graduates/(\d+)/toggle-status$~' => 'admin/toggle-graduate-status.php',
    '~^/admin/companies/(\d+)$~' => 'admin/view-company.php',
    '~^/admin/companies/(\d+)/edit$~' => 'admin/edit-company.php',
    '~^/admin/companies/(\d+)/delete$~' => 'admin/delete-company.php',
    '~^/admin/users/(\d+)$~' => 'admin/view-user.php',
    '~^/admin/users/(\d+)/edit$~' => 'admin/edit-user.php',
    '~^/admin/users/(\d+)/delete$~' => 'admin/delete-user.php',
    
    // Company dynamic routes
    '~^/company/jobs/(\d+)$~' => 'company/view-job.php',
    '~^/company/jobs/(\d+)/edit$~' => 'company/edit-job.php',
    '~^/company/jobs/(\d+)/applications$~' => 'company/job-applications.php',
    
    // Student dynamic routes
    '~^/student/jobs/(\d+)$~' => 'student/view-job.php',
    '~^/student/notifications/(\d+)$~' => 'student/view-notification.php',
    
    // Graduate dynamic routes
    '~^/graduate/jobs/(\d+)$~' => 'graduate/view-job.php',
    '~^/graduate/notifications/(\d+)$~' => 'graduate/view-notification.php',
    
    // Teacher dynamic routes
    '~^/teacher/jobs/(\d+)$~' => 'teacher/job-detail.php',
    '~^/teacher/students/(\d+)$~' => 'teacher/student-detail.php',
];

foreach ($patterns as $pattern => $file) {
    if (preg_match($pattern, $uri, $matches)) {
        $file_path = __DIR__ . '/' . $file;
        if (file_exists($file_path)) {
            // Set the ID parameter for the matched route
            if (isset($matches[1])) {
                $_GET['id'] = $matches[1];
            }
            require_once $file_path;
            return true;
        }
    }
}

// Default fallback - serve index.php or return 404
if ($uri === '' || $uri === '/') {
    require_once __DIR__ . '/index.php';
    return true;
}

// Return false to let PHP handle it (will result in 404 for non-existent files)
return false;