<?php
/**
 * File Path: includes/migrations/class-mbs-migration-2-6.php
 *
 * MBS_Migration_2_6 — Sprint F-020 — PUB supply chain columns
 *
 * Add price_type, currency_code, supplier_name, discount_code, on_sale_date,
 * and returnability columns to mbs_format_rows (ADD COLUMN IF NOT EXISTS).
 *
 * @package MyBookShowroom
 * @since   3.2.0
 */

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

/**
 * Class MBS_Migration_2_6
 *
 * Add price_type, currency_code, supplier_name, discount_code, on_sale_date,
 * and returnability columns to mbs_format_rows (ADD COLUMN IF NOT EXISTS).
 *
 * @implements MBS_Migration
 */
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- DAO/migration: table names via $wpdb->prefix are trusted server-controlled values, not user input
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter -- same reason: $wpdb->prefix table names are safe; $where_sql is built from sanitized server-controlled inputs
class MBS_Migration_2_6 implements MBS_Migration { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * {@inheritdoc}
	 *
	 * @return string
	 */
	public function version(): string {
		return '2.6';
	}

	/**
	 * {@inheritdoc}
	 *
	 * Add price_type, currency_code, supplier_name, discount_code, on_sale_date,
 * and returnability columns to mbs_format_rows (ADD COLUMN IF NOT EXISTS).
	 *
	 * @return void
	 */
	public function up(): void {
		global $wpdb;
		$fmt_table = $wpdb->prefix . 'mbs_format_rows';
		$fmt_cols = $wpdb->get_col( "SHOW COLUMNS FROM {$fmt_table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- DAO layer -- direct DB access is intentional per MBS architecture
		$fmt_adds = array();

		if ( ! in_array( 'price_type',    $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN IF NOT EXISTS `price_type`    VARCHAR(2)   NULL DEFAULT NULL';
		if ( ! in_array( 'currency_code', $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN IF NOT EXISTS `currency_code` CHAR(3)      NULL DEFAULT NULL';
		if ( ! in_array( 'supplier_name', $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN IF NOT EXISTS `supplier_name` VARCHAR(255) NULL DEFAULT NULL';
		if ( ! in_array( 'discount_code', $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN IF NOT EXISTS `discount_code` VARCHAR(50)  NULL DEFAULT NULL';
		if ( ! in_array( 'on_sale_date',  $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN IF NOT EXISTS `on_sale_date`  DATE         NULL DEFAULT NULL';
		if ( ! in_array( 'returnability', $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN IF NOT EXISTS `returnability` VARCHAR(2)   NULL DEFAULT NULL';

		if ( ! empty( $fmt_adds ) ) {
			$wpdb->query( 'ALTER TABLE `' . $fmt_table . '` ' . implode( ', ', $fmt_adds ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.NotPrepared -- DAO layer -- direct DB access is intentional per MBS architecture
		}
	}
}
