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

namespace Awesomemotive\Rewardswp\Database\Upgrades;

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

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

/**
 * Adds context column to the events table.
 *
 * This upgrade adds a context column to provide more granular event tracking.
 * The context column allows for better categorization of events while maintaining
 * backward compatibility with existing event types.
 *
 * @since 1.1.0
 */
/**
 * Class AddContextColumnToEventsTableUpgrade.
 */
class AddContextColumnToEventsTableUpgrade 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;

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

	/**
	 * Run the upgrade.
	 *
	 * Adds the context column to the events table. The column is nullable
	 * to maintain backward compatibility with existing events.
	 *
	 * @since 1.1.0
	 * @return void
	 */
	public function up(): void {
		// Add the context column after the `type` column.
		$this->table->add_column(
			$this->table->get_prefixed_table_name(),
			'context',
			'varchar(50) DEFAULT NULL',
			'type'
		);
	}
}
