<?php

spl_autoload_register(function ($class) {
    // Map namespace/class to directory structure
    // e.g., App\Controllers\PurchaseController -> app/Controllers/PurchaseController.php
    
    // Remove 'App\' prefix if present
    if (strpos($class, 'App\\') === 0) {
        $class = substr($class, 4);
    }
    
    $file = __DIR__ . '/../' . str_replace('\\', '/', $class) . '.php';
    
    if (file_exists($file)) {
        require_once $file;
        return true;
    }
    return false;
});