<?php
/**
 * SalonPro X - PSR-4 Style Autoloader
 * Automatically loads classes from core/ and modules/ directories
 */

spl_autoload_register(function (string $class): void {
    $prefix = 'core\\';
    $baseDir = __DIR__ . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR;
    
    // Check if class starts with core\ namespace
    if (str_starts_with($class, $prefix)) {
        $relativeClass = substr($class, strlen($prefix));
        $file = $baseDir . str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass) . '.php';
        if (file_exists($file)) {
            require_once $file;
            return;
        }
    }
    
    // Handle module classes: Modules\{ModuleName}\{Type}\{ClassName}
    $modulePrefix = 'Modules\\';
    if (str_starts_with($class, $modulePrefix)) {
        $relativeClass = substr($class, strlen($modulePrefix));
        $file = __DIR__ . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass) . '.php';
        if (file_exists($file)) {
            require_once $file;
            return;
        }
    }
    
    // Handle middleware namespace
    $middlewarePrefix = 'middleware\\';
    $middlewareBaseDir = __DIR__ . DIRECTORY_SEPARATOR . 'middleware' . DIRECTORY_SEPARATOR;
    if (str_starts_with($class, $middlewarePrefix)) {
        $relativeClass = substr($class, strlen($middlewarePrefix));
        $file = $middlewareBaseDir . str_replace('\\', DIRECTORY_SEPARATOR, $relativeClass) . '.php';
        if (file_exists($file)) {
            require_once $file;
            return;
        }
    }
});
