<?php

declare(strict_types=1);

/**
 * Simple PSR-4-style autoloader for app MVC classes.
 */
spl_autoload_register(static function (string $class): void {
    $prefixes = [
        'App\\Services\\' => __DIR__ . '/../services/',
        'App\\Models\\' => __DIR__ . '/../models/',
        'App\\Controllers\\' => __DIR__ . '/../controllers/',
        'App\\Middleware\\' => __DIR__ . '/../middleware/',
    ];

    foreach ($prefixes as $prefix => $baseDir) {
        if (!str_starts_with($class, $prefix)) {
            continue;
        }

        $relative = substr($class, strlen($prefix));
        $file = $baseDir . str_replace('\\', '/', $relative) . '.php';

        if (is_file($file)) {
            require_once $file;
        }
    }
});
