<?php

declare(strict_types=1);

/**
 * PSR-4 Autoloader — Gmind Institute
 *
 * Manual PSR-4 autoloader for the application namespaces.
 * No Composer required — this file handles all class loading.
 *
 * Namespace mappings:
 *   App\         → /app/
 *   Admin\       → /admin/         (existing website admin panel)
 *   SuperAdmin\  → /super-admin/   (NEW — Super Admin panel for branch management)
 *   BranchAdmin\ → /branch/        (NEW — Branch Admin panel)
 *   Core\        → /app/Core/      (alias — framework core)
 */

$projectRoot = dirname(__DIR__);

spl_autoload_register(function (string $className) use ($projectRoot): void {
    // Define namespace prefixes and their corresponding base directories.
    // Order matters: longer prefixes first to avoid partial matches.
    $prefixes = [
        'App\\'         => $projectRoot . '/app/',
        'Core\\'        => $projectRoot . '/app/Core/',
        'Admin\\'       => $projectRoot . '/admin/',
        'SuperAdmin\\'  => $projectRoot . '/super-admin/',
        'BranchAdmin\\' => $projectRoot . '/branch/',
    ];

    foreach ($prefixes as $prefix => $baseDir) {
        $prefixLen = strlen($prefix);
        if (strncmp($prefix, $className, $prefixLen) !== 0) {
            continue;
        }

        $relativeClass = substr($className, $prefixLen);
        $file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';

        if (file_exists($file)) {
            require $file;
            return;
        }
    }
});
