<?php
/**
 * WP-CLI command registration for WP PowerSuite.
 *
 * Registered only in a WP-CLI context. Command classes are PSR-4 autoloaded and
 * instantiated lazily by WP-CLI, so nothing here runs during normal web/admin
 * requests.
 *
 * @package WP_PowerSuite\CLI
 * @since 1.0.7
 */

namespace WP_PowerSuite\CLI;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Registers the `wp powersuite ...` command tree.
 *
 * @since 1.0.7
 */
final class CommandLoader {

	/**
	 * Whether commands have already been registered (idempotency guard).
	 *
	 * @var bool
	 */
	private static bool $registered = false;

	/**
	 * Register all WP PowerSuite CLI commands.
	 *
	 * Safe to call more than once. Does nothing outside WP-CLI.
	 *
	 * @since 1.0.7
	 * @return void
	 */
	public static function register(): void {
		if ( self::$registered || ! class_exists( 'WP_CLI', false ) ) {
			return;
		}

		self::$registered = true;

		$commands = [
			'powersuite status'       => StatusCommand::class,
			'powersuite module'       => ModuleCommand::class,
			'powersuite settings'     => SettingsCommand::class,
			'powersuite cache'        => CacheCommand::class,
			'powersuite database'     => DatabaseCommand::class,
			'powersuite broken-links' => BrokenLinksCommand::class,
			'powersuite redirects'    => RedirectsCommand::class,
			'powersuite backup'       => BackupCommand::class,
			'powersuite cron'         => CronCommand::class,
		];

		foreach ( $commands as $name => $class ) {
			\WP_CLI::add_command( $name, $class );
		}
	}
}
