<?php
namespace Gestoreasy;

class Autoloader {
    public static function register(): void {
        spl_autoload_register(function ($class) {
            // PSR-4-like autoload for Gestoreasy\*
            $gePrefix = 'Gestoreasy\\';
            $geBaseDir = __DIR__ . '/';
            $geLen = strlen($gePrefix);
            if (strncmp($gePrefix, $class, $geLen) === 0) {
                $relativeClass = substr($class, $geLen);
                $file = $geBaseDir . str_replace('\\', '/', $relativeClass) . '.php';
                if (file_exists($file)) {
                    require $file;
                    return;
                }
            }
            // Autoload for Modules\* under project modules directory
            $modPrefix = 'Modules\\';
            $modLen = strlen($modPrefix);
            if (strncmp($modPrefix, $class, $modLen) === 0) {
                // modules dir is two levels up from src
                $projectRoot = realpath(__DIR__ . '/..') ?: dirname(__DIR__);
                $modulesDir = $projectRoot . '/modules/';
                $relativeClass = substr($class, $modLen);
                $file = $modulesDir . str_replace('\\', '/', $relativeClass) . '.php';
                if (file_exists($file)) {
                    require $file;
                    return;
                }
            }
        });
    }
}