<?php
/**
 * Migration Runner.
 *
 * @package Ugcify
 */

namespace Ugcify\Database\Migrations;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Migration Runner class.
 */
class MigrationRunner {

	/**
	 * WordPress database instance.
	 *
	 * @var \wpdb
	 */
	private $wpdb;

	/**
	 * Constructor.
	 *
	 * @param \wpdb $wpdb WordPress database instance.
	 */
	public function __construct( $wpdb ) {
		$this->wpdb = $wpdb;
	}

	/**
	 * Run all migrations.
	 *
	 * @return void
	 */
	public function run() {
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		$migrations = array(
			new CreateReviewsTable( $this->wpdb ),
			new CreateReviewMetaTable( $this->wpdb ),
			new CreateReviewSyncMapTable( $this->wpdb ),
			new CreateProductTagsTable( $this->wpdb ),
			new CreatePointsTable( $this->wpdb ),
			new CreateAutomationLogsTable( $this->wpdb ),
			new CreateAnalyticsTable( $this->wpdb ),
			new CreateInfluencerInvitationsTable( $this->wpdb ),
		);

		foreach ( $migrations as $migration ) {
			$sql = $migration->run();
			dbDelta( $sql );
		}

		// Procedural alter-table migrations (call run() directly — they handle their own queries).
		$procedural_migrations = array(
			new AddActionToPointsTable( $this->wpdb ),
			new AlterPointsTableAddTransactionFields( $this->wpdb ),
		);

		foreach ( $procedural_migrations as $migration ) {
			$migration->run();
		}
	}
}
