<?php
/**
 * Migration: add bundle_discount_method column to WooCommerce presets.
 *
 * Allows choosing between coupon-based and line-item pricing for bundles.
 * Default 'coupon' preserves existing behaviour.
 *
 * @package BoostBot
 * @since   2.1.2
 */

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Migration owns custom-table DDL. Table names pass through sanitize_identifier(); DDL cannot use prepare() placeholders for identifiers; caching is irrelevant for one-shot migrations.
defined( 'ABSPATH' ) || exit;

class BoostBot_Migration_Add_Bundle_Discount_Method {

	/**
	 * Run the migration.
	 *
	 * @return void
	 */
	public static function run() {
		( new self() )->up();
	}

	/**
	 * Add the column.
	 *
	 * @return void
	 */
	public function up() {
		global $wpdb;

		$table = self::sanitize_identifier( $wpdb->prefix . 'boostbot_woocommerce_presets' );

		if ( ! $this->table_exists( $table ) || $this->column_exists( $table, 'bundle_discount_method' ) ) {
			return;
		}

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- DDL cannot use placeholders for identifiers; $table is sanitized, definitions are literals.
		$wpdb->query( "ALTER TABLE {$table} ADD COLUMN bundle_discount_method varchar(20) NOT NULL DEFAULT 'coupon' AFTER bundle_discount_value" );
	}

	/**
	 * Check whether the table exists.
	 *
	 * @param string $table Table name.
	 * @return bool
	 */
	private function table_exists( string $table ): bool {
		global $wpdb;

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$result = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) );

		return $table === $result;
	}

	/**
	 * Check whether a column exists.
	 *
	 * @param string $table  Table name.
	 * @param string $column Column name.
	 * @return bool
	 */
	private function column_exists( string $table, string $column ): bool {
		global $wpdb;

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- DDL cannot use placeholders for identifiers; $table is sanitized, definitions are literals.
		$results = $wpdb->get_results( $wpdb->prepare( "SHOW COLUMNS FROM {$table} LIKE %s", $column ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		return ! empty( $results );
	}

    /**
     * Restrict a SQL identifier to [A-Za-z0-9_] characters.
     *
     * DDL cannot use $wpdb->prepare() for identifiers, so any identifier
     * that ends up interpolated into a query first passes through this
     * allow-list.
     *
     * @param string $identifier Raw identifier.
     * @return string Identifier with disallowed characters removed.
     */
    private static function sanitize_identifier( $identifier ) {
        return preg_replace( '/[^A-Za-z0-9_]/', '', (string) $identifier );
    }
}
