<?php

declare(strict_types=1);

/**
 * Test bootstrap: load Composer autoloader and register stubs for
 * internal Nextcloud classes that OCP interfaces reference but that
 * are not shipped with the nextcloud/ocp stubs package.
 */

// Stub OC\Hooks\Emitter (referenced by OCP\Files\IRootFolder)
if (!interface_exists('OC\Hooks\Emitter')) {
    // @phpcs:ignore PSR1.Classes.ClassDeclaration.MissingNamespace
    eval('namespace OC\Hooks; interface Emitter {}');
}

// Stub OCA\DAV\CalDAV\CalDavBackend (from the dav app, not in OCP stubs)
if (!class_exists('OCA\DAV\CalDAV\CalDavBackend')) {
    eval('namespace OCA\DAV\CalDAV; class CalDavBackend {}');
}

// Try the app's vendor autoload first (full local dev).
// Fall back to Nextcloud's root autoloader which has PSR packages.
$appAutoload = __DIR__ . '/../vendor/autoload.php';
$ncAutoload = '/var/www/html/lib/composer/autoload.php';

if (file_exists($appAutoload)) {
    require_once $appAutoload;
} elseif (file_exists($ncAutoload)) {
    require_once $ncAutoload;
}

// Register the app's lib directory for PSR-4 autoloading.
// This ensures OCA\Mitgliederverwaltung\* classes are loaded regardless
// of which vendor/autoload.php was used (full local dev vs. container).
spl_autoload_register(function ($class) {
    $prefix = 'OCA\\Mitgliederverwaltung\\';
    $baseDir = __DIR__ . '/../lib/';
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        return;
    }
    $relativeClass = substr($class, $len);
    $file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
    if (file_exists($file)) {
        require $file;
    }
});
