<?php

// Resolve full application URLs
function url($path = '') {
    $baseUrl = rtrim(APP_URL, '/');
    $path = ltrim($path, '/');
    
    // Auto-prefix for Staff
    if (isset($_SESSION['user_role']) && $_SESSION['user_role'] === 'staff') {
        if ($path === 'dashboard') {
            $path = 'staff/dashboard';
        } else if (strpos($path, 'admin/') === 0) {
            // Check if this is a path that staff has access to
            $staffPaths = ['admin/students', 'admin/leads', 'admin/admissions', 'admin/attendance', 'admin/teachers', 'admin/courses', 'admin/batches', 'admin/study-materials', 'admin/categories', 'admin/fee-heads', 'admin/fees', 'admin/expenses', 'admin/certificates'];
            $isStaffPath = false;
            foreach ($staffPaths as $sp) {
                if (strpos($path, $sp) === 0) {
                    $isStaffPath = true;
                    break;
                }
            }
            
            if ($isStaffPath) {
                $path = str_replace('admin/', 'staff/', $path);
            }
        }
    }
    
    return $baseUrl . '/' . $path;
}

// Quick redirect helper
function redirect($path) {
    if (strpos($path, 'http://') === 0 || strpos($path, 'https://') === 0) {
        header('Location: ' . $path);
    } else {
        header('Location: ' . url($path));
    }
    exit;
}
