<?php

require_once ROOT_PATH . '/config/config.php';

// --- Core classes ---
require_once APP_PATH . '/core/Database.php';
require_once APP_PATH . '/core/Router.php';
require_once APP_PATH . '/core/AuditLogger.php';
require_once APP_PATH . '/core/Auth.php';
require_once APP_PATH . '/core/Controller.php';
require_once APP_PATH . '/core/Model.php';

// --- Autoload models on demand ---
spl_autoload_register(function (string $class) {
    $modelFile = APP_PATH . '/models/' . $class . '.php';
    if (file_exists($modelFile)) {
        require_once $modelFile;
    }
});

// --- Helper functions available everywhere ---
function e(?string $value): string
{
    return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8');
}

function formatMoney(float $amount): string
{
    return APP_CURRENCY_SYMBOL . ' ' . number_format($amount, 2);
}

function setting(string $key, $default = null)
{
    static $settings = null;
    if ($settings === null) {
        $settings = [];
        $rows = Database::getConnection()->query("SELECT setting_key, setting_value FROM settings")->fetchAll();
        foreach ($rows as $row) {
            $settings[$row['setting_key']] = $row['setting_value'];
        }
    }
    return $settings[$key] ?? $default;
}
