<?php
/**
 * Universal System Initializer
 * This file should be included at the top of EVERY page
 */

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Calculate the correct base path based on file location
if (!defined('BASE_PATH')) {
    // Start from the current file's directory
    $current_dir = __DIR__; // This is /HMSPOS/config/
    
    // Go up one level to get to HMSPOS root
    define('BASE_PATH', dirname($current_dir));
}

// Load core configuration first
$config_file = BASE_PATH . '/config/config.php';
if (file_exists($config_file)) {
    require_once $config_file;
} else {
    die("Critical error: config.php not found at: " . $config_file);
}

// Load database functions
$db_file = BASE_PATH . '/config/database.php';
if (file_exists($db_file)) {
    require_once $db_file;
} else {
    die("Critical error: database.php not found at: " . $db_file);
}

// Load auth functions
$auth_file = BASE_PATH . '/config/auth.php';
if (file_exists($auth_file)) {
    require_once $auth_file;
} else {
    die("Critical error: auth.php not found at: " . $auth_file);
}

// Load optional files (they might not exist)
if (file_exists(BASE_PATH . '/config/constants.php')) {
    require_once BASE_PATH . '/config/constants.php';
}
if (file_exists(BASE_PATH . '/config/helpers.php')) {
    require_once BASE_PATH . '/config/helpers.php';
}
if (file_exists(BASE_PATH . '/config/csrf.php')) {
    require_once BASE_PATH . '/config/csrf.php';
}

// Ensure session is started
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Format currency
if (!function_exists('format_currency')) {
    function format_currency($amount) {
        return 'RWF ' . number_format($amount, 2);
    }
}