<?php
/**
 * WIMAS - System Bootstrap & Configuration
 * Location: /config/bootstrap.php
 */

// --- 1. SESSION MANAGEMENT ---
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// --- 2. SYSTEM PATHS ---
// Create a robust absolute path using __DIR__
// realpath() resolves all symbolic links and '/../' to give a clean absolute string
define('BASE_PATH', realpath(__DIR__ . '/../') . '/');

// --- 3. SECURITY & ENCRYPTION ---
define('ENCRYPTION_KEY', 'WIMAS_SECURE_PORTAL_2026'); 
define('ENCRYPTION_METHOD', 'AES-256-CBC');

// Helper function to encrypt URL parameters
function encrypt_url($data) {
    $iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(ENCRYPTION_METHOD));
    $encrypted = openssl_encrypt($data, ENCRYPTION_METHOD, ENCRYPTION_KEY, 0, $iv);
    return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($encrypted . '::' . $iv));
}

// Helper function to decrypt URL parameters
function decrypt_url($data) {
    $data = base64_decode(str_replace(['-', '_'], ['+', '/'], $data));
    if (strpos($data, '::') !== false) {
        list($encrypted_data, $iv) = explode('::', $data, 2);
        return openssl_decrypt($encrypted_data, ENCRYPTION_METHOD, ENCRYPTION_KEY, 0, $iv);
    }
    return false;
}

// --- 4. DATABASE INITIALIZATION ---
// Auto-load Database using the absolute path
require_once BASE_PATH . 'config/database.php';

try {
    $db = new Database(); 
} catch (Exception $e) {
    die("Global System Error: " . $e->getMessage());
}

// --- 5. GLOBAL SYSTEM MESSAGES ---
$system_messages = [
    // Success Messages
    'success'         => ['type' => 'success', 'icon' => 'fa-check-circle', 'title' => 'Success', 'msg' => 'Operation completed successfully.'],
    'added'           => ['type' => 'success', 'icon' => 'fa-plus-circle',  'title' => 'Added',   'msg' => 'New record has been successfully created.'],
    'updated'         => ['type' => 'info',    'icon' => 'fa-save',         'title' => 'Updated', 'msg' => 'Record has been successfully updated.'],
    'deleted'         => ['type' => 'warning', 'icon' => 'fa-trash-alt',    'title' => 'Deleted', 'msg' => 'Record has been permanently removed.'],
    
    // Validation & Warnings
    'duplicate'       => ['type' => 'warning', 'icon' => 'fa-exclamation-triangle', 'title' => 'Duplicate', 'msg' => 'This record already exists in the system.'],
    'empty'           => ['type' => 'warning', 'icon' => 'fa-indent',      'title' => 'Required',  'msg' => 'Please fill in all required fields.'],
    
    // Error & Database Failures
    'db_error'        => ['type' => 'danger',  'icon' => 'fa-database',    'title' => 'DB Error',  'msg' => 'Database transaction failed. Please try again.'],
    'access_denied'   => ['type' => 'danger',  'icon' => 'fa-shield-alt',  'title' => 'Denied',    'msg' => 'You do not have permission for this action.']
];
?>