<?php
/**
 * Bootstrap — Autoloader, session init, helper loading
 */

$appConfig = require ROOT_PATH . '/config/app.php';
date_default_timezone_set($appConfig['timezone']);

// Autoloader
spl_autoload_register(function ($class) {
    // Core classes
    $coreFile = ROOT_PATH . '/core/' . $class . '.php';
    if (file_exists($coreFile)) {
        require_once $coreFile;
        return;
    }

    // Middleware
    $middlewareFile = ROOT_PATH . '/core/Middleware/' . $class . '.php';
    if (file_exists($middlewareFile)) {
        require_once $middlewareFile;
        return;
    }

    // Module controllers: format ModuleName\Controllers\ClassName
    // We resolve by scanning modules
    $parts = explode('\\', $class);
    if (count($parts) >= 3) {
        $moduleName = $parts[0];
        $subDir = $parts[1];
        $className = $parts[2];
        $moduleFile = ROOT_PATH . '/modules/' . strtolower($moduleName) . '/' . $subDir . '/' . $className . '.php';
        if (file_exists($moduleFile)) {
            require_once $moduleFile;
            return;
        }
    }
});

// Load helpers
require_once ROOT_PATH . '/core/helpers.php';

// Start session
Session::start();