<?php
spl_autoload_register( 'forumax_autoloader' );

/**
 * Autoload files for the Forumax plugin.
 *
 * SCOPED: Only handles classes that belong to Forumax.
 * This prevents conflicts with other plugins' autoloaders and namespaces.
 *
 * @param string $class The fully-qualified class name.
 * @return void
 */
function forumax_autoloader( $class ) {
	// Define the namespace/class prefixes that belong to Forumax.
	$forumax_prefixes = array(
		'admin\\',      // admin\Menu, admin\Elementor\*, admin\Blocks\*, admin\menu\*
		'Admin\\',      // Admin\Admin_Assets
		'Frontend\\',   // Frontend\Frontend_Assets
		'features\\',   // features\bbp_voting, features\bbp_attachments, etc.
	);

	// Check if this class belongs to Forumax.
	$is_forumax_class = false;

	// Check for exact global class name (no namespace).
	if ( 'Admin' === $class ) {
		$is_forumax_class = true;
	} else {
		// Check for namespace prefixes.
		foreach ( $forumax_prefixes as $prefix ) {
			if ( strpos( $class, $prefix ) === 0 ) {
				$is_forumax_class = true;
				break;
			}
		}
	}

	// Bail early if this class doesn't belong to Forumax.
	if ( ! $is_forumax_class ) {
		return;
	}

	$path = __DIR__ . '/includes/' . str_replace( '\\', '/', $class ) . '.php';

	if ( file_exists( $path ) ) {
		include_once $path;
	}
}