<?php
/**
 * RenameAdvocateIdToMemberIdInEventsTableUpgrade.php.
 *
 * @package RewardsWP
 */

namespace Awesomemotive\Rewardswp\Database\Upgrades;

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

use Awesomemotive\Rewardswp\Abstracts\AbstractDatabaseUpgrade;
use Awesomemotive\Rewardswp\Database\Tables\EventsTable;
use Awesomemotive\Rewardswp\GlobalConfig;

/**
 * Renames advocate_id column to member_id in the events table.
 *
 * This upgrade reflects the broader usage of events beyond just referral tracking.
 * Members can create events that aren't necessarily tied to referrals (e.g., points program events).
 *
 * @since 1.1.0
 */
/**
 * Class RenameAdvocateIdToMemberIdInEventsTableUpgrade.
 */
class RenameAdvocateIdToMemberIdInEventsTableUpgrade extends AbstractDatabaseUpgrade {

	/**
	 * Upgrade target version.
	 *
	 * @var string
	 */
	protected string $version = '1.1.0';

	/**
	 * Events table instance.
	 *
	 * @since 1.1.0
	 * @var EventsTable
	 */
	private EventsTable $table;

	/**
	 * Global config instance.
	 *
	 * @since 1.1.0
	 * @var GlobalConfig
	 */
	private GlobalConfig $global_config;

	/**
	 * Constructor.
	 *
	 * @since 1.1.0
	 * @param EventsTable  $table         Events table instance.
	 * @param GlobalConfig $global_config Global config instance.
	 */
	public function __construct( EventsTable $table, GlobalConfig $global_config ) {
		$this->table         = $table;
		$this->global_config = $global_config;
	}

	/**
	 * Run the upgrade.
	 *
	 * Renames the advocate_id column to member_id to reflect the broader
	 * usage of events in the system.
	 *
	 * @since 1.1.0
	 * @return void
	 */
	public function up(): void {
		$db         = $this->global_config->get_db();
		$table_name = $this->table->get_prefixed_table_name();

		// Check if the advocate_id column exists.
		$column_exists = $db->get_var(
			$db->prepare(
				'SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = %s',
				DB_NAME,
				$table_name,
				'advocate_id'
			)
		);

		// Check if member_id already exists.
		$member_id_exists = $db->get_var(
			$db->prepare(
				'SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = %s',
				DB_NAME,
				$table_name,
				'member_id'
			)
		);

		// Only proceed if advocate_id exists and member_id doesn't.
		if ( $column_exists && ! $member_id_exists ) {
			// Check if the advocate_id index exists.
			$index_exists = $db->get_var(
				$db->prepare(
					'SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND INDEX_NAME = %s',
					DB_NAME,
					$table_name,
					'advocate_id'
				)
			);

			// Drop the old index first if it exists.
			if ( $index_exists ) {
				$db->query(
					sprintf(
						'ALTER TABLE `%s` DROP INDEX `advocate_id`',
						$table_name
					)
				);
			}

			// Rename the column.
			$db->query(
				sprintf(
					'ALTER TABLE `%s` CHANGE COLUMN `advocate_id` `member_id` bigint(20) unsigned NULL',
					$table_name
				)
			);

			// Add the new index.
			$db->query(
				sprintf(
					'ALTER TABLE `%s` ADD INDEX `member_id` (`member_id`)',
					$table_name
				)
			);
		}
	}
}
