<?php
/**
 * Migrations Manager
 *
 * @package AmbassadorHub\Migrations
 */

namespace AmbassadorHub\Migrations;

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

/**
 * Coordinates database migrations across all modules.
 *
 * Fires `ambh_run_migrations` on every admin_init. Each module's
 * AbstractMigrationRunner subclass listens to that hook and skips
 * any version it has already applied — so this is safe to call
 * unconditionally with no global version gate.
 */
final class Manager {
	/**
	 * Initialize migrations manager.
	 *
	 * @return void
	 */
	public static function init(): void {
		add_action( 'admin_init', array( self::class, 'run_migrations' ) );
	}

	/**
	 * Fire the platform migration hook so every registered module runner
	 * can apply any pending steps.
	 *
	 * Each module runner is individually idempotent — it tracks its own
	 * schema version in wp_options and skips already-applied migrations.
	 *
	 * @return void
	 */
	public static function run_migrations(): void {
		/**
		 * Fires when the migration coordinator runs.
		 *
		 * Module MigrationRunners hook here via AbstractMigrationRunner::init().
		 *
		 * @since 0.1.0
		 */
		do_action( 'ambh_run_migrations' );

		/**
		 * Fires after all module migration runners have been invoked.
		 *
		 * @since 0.1.0
		 */
		do_action( 'ambh_migrations_completed' );
	}
}
