<?php
/**
 * Autoloader for MVC classes
 * Automatically loads Model, Controller, and other classes
 */

spl_autoload_register(function ($class) {
    // Base directory for the namespace prefix
    $baseDir = __DIR__ . '/../app/';
    
    // Convert namespace to file path
    $file = $baseDir . str_replace('\\', '/', $class) . '.php';
    
    // If the file exists, require it
    if (file_exists($file)) {
        require $file;
        return;
    }
    
    // Try models directory
    $modelFile = $baseDir . 'models/' . $class . '.php';
    if (file_exists($modelFile)) {
        require $modelFile;
        return;
    }
    
    // Try controllers directory
    $controllerFile = $baseDir . 'controllers/' . $class . '.php';
    if (file_exists($controllerFile)) {
        require $controllerFile;
        return;
    }
    
    // Try services directory
    $serviceFile = $baseDir . 'services/' . $class . '.php';
    if (file_exists($serviceFile)) {
        require $serviceFile;
        return;
    }
    
    // Try middleware directory
    $middlewareFile = $baseDir . 'middleware/' . $class . '.php';
    if (file_exists($middlewareFile)) {
        require $middlewareFile;
        return;
    }
    
    // Try helpers directory
    $helperFile = $baseDir . 'helpers/' . $class . '.php';
    if (file_exists($helperFile)) {
        require $helperFile;
        return;
    }
});
