<?php
/**
 * Lightweight PSR-style autoloader for FindAstra_* classes.
 *
 * Maps FindAstra_Foo_Bar to includes/class-findastra-foo-bar.php
 * and FindAstra_Provider_OpenAI to includes/providers/class-findastra-provider-openai.php.
 *
 * @package FindAstra
 */

defined( 'ABSPATH' ) || exit;

final class FindAstra_Autoloader {

	public static function register(): void {
		spl_autoload_register( array( __CLASS__, 'load' ) );
	}

	public static function load( string $class ): void {
		if ( strpos( $class, 'FindAstra_' ) !== 0 ) {
			return;
		}

		$slug          = strtolower( str_replace( '_', '-', $class ) );
		$filename      = 'class-' . $slug . '.php';
		// Freemius strips files whose basename ends in __premium_only before
		// the extension out of the free build. Look for the suffixed copy
		// first so the premium build picks it up; fall back to the plain
		// name (used by free-tier providers like Local and Mock).
		$filename_paid = 'class-' . $slug . '__premium_only.php';

		$candidates = array(
			FINDASTRA_DIR . 'includes/' . $filename_paid,
			FINDASTRA_DIR . 'includes/providers/' . $filename_paid,
			FINDASTRA_DIR . 'includes/' . $filename,
			FINDASTRA_DIR . 'includes/providers/' . $filename,
		);

		foreach ( $candidates as $path ) {
			if ( is_readable( $path ) ) {
				require_once $path;
				return;
			}
		}
	}
}
