<?php
/**
 * File Path: includes/migrations/class-mbs-migration-1-7.php
 *
 * MBS_Migration_1_7 — Sprint F — Format & Contributors expansion
 *
 * Reconcile installer/DAO schema mismatch. ADD columns to mbs_format_rows and
 * mbs_buy_links. Create mbs_contributors table via dbDelta.
 *
 * @package MyBookShowroom
 * @since   3.2.0
 */

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

/**
 * Class MBS_Migration_1_7
 *
 * Reconcile installer/DAO schema mismatch. ADD columns to mbs_format_rows and
 * mbs_buy_links. Create mbs_contributors table via dbDelta.
 *
 * @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_1_7 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 '1.7';
	}

	/**
	 * {@inheritdoc}
	 *
	 * Reconcile installer/DAO schema mismatch. ADD columns to mbs_format_rows and
 * mbs_buy_links. Create mbs_contributors table via dbDelta.
	 *
	 * @return void
	 */
	public function up(): void {
		global $wpdb;
		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		$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( 'subtitle',              $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN subtitle              VARCHAR(500)     NULL DEFAULT NULL AFTER format_label';
		if ( ! in_array( 'pub_date',              $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN pub_date              DATE             NULL DEFAULT NULL AFTER subtitle';
		if ( ! in_array( 'edition_notes',         $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN edition_notes         VARCHAR(500)     NULL DEFAULT NULL AFTER pub_date';
		if ( ! in_array( 'narrator',              $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN narrator              VARCHAR(255)     NULL DEFAULT NULL AFTER edition_notes';
		if ( ! in_array( 'duration_hh',           $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN duration_hh           TINYINT UNSIGNED NULL DEFAULT NULL AFTER narrator';
		if ( ! in_array( 'duration_mm',           $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN duration_mm           TINYINT UNSIGNED NULL DEFAULT NULL AFTER duration_hh';
		if ( ! in_array( 'file_format',           $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN file_format           VARCHAR(50)      NULL DEFAULT NULL AFTER duration_mm';
		if ( ! in_array( 'audio_sample_id',       $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN audio_sample_id       BIGINT UNSIGNED  NULL DEFAULT NULL AFTER audio_sample_url';
		if ( ! in_array( 'author_freetext',       $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN author_freetext       VARCHAR(255)     NULL DEFAULT NULL';
		if ( ! in_array( 'author_term_id',        $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN author_term_id        BIGINT UNSIGNED  NULL DEFAULT NULL';
		if ( ! in_array( 'series_term_id',        $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN series_term_id        BIGINT UNSIGNED  NULL DEFAULT NULL';
		if ( ! in_array( 'series_freetext',       $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN series_freetext       VARCHAR(255)     NULL DEFAULT NULL';
		if ( ! in_array( 'series_number',         $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN series_number         DECIMAL(5,2)     NULL DEFAULT NULL';
		if ( ! in_array( 'audiobook_cover_style', $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN audiobook_cover_style VARCHAR(50)      NULL DEFAULT NULL';
		if ( ! in_array( 'spinning_video_id',     $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN spinning_video_id     BIGINT UNSIGNED  NULL DEFAULT NULL';
		if ( ! in_array( 'video_hover_mode',      $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN video_hover_mode      TINYINT(1)       NOT NULL DEFAULT 0';
		if ( ! in_array( 'autographed_available', $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN autographed_available TINYINT(1)       NOT NULL DEFAULT 0';
		if ( ! in_array( 'is_deleted',            $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN is_deleted            TINYINT(1)       NOT NULL DEFAULT 0 AFTER sort_order';
		if ( ! in_array( 'deleted_at',            $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN deleted_at            DATETIME         NULL DEFAULT NULL AFTER is_deleted';
		if ( ! in_array( 'created_at',            $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN created_at            DATETIME         NULL DEFAULT NULL AFTER deleted_at';
		if ( ! in_array( 'updated_at',            $fmt_cols, true ) ) $fmt_adds[] = 'ADD COLUMN updated_at            DATETIME         NULL DEFAULT NULL AFTER created_at';

		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
		}

		$bl_table = $wpdb->prefix . 'mbs_buy_links';
		$bl_cols = $wpdb->get_col( "SHOW COLUMNS FROM {$bl_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
		$bl_adds = array();

		if ( ! in_array( 'retailer_key',     $bl_cols, true ) ) $bl_adds[] = 'ADD COLUMN retailer_key     VARCHAR(50)  NULL DEFAULT NULL';
		if ( ! in_array( 'custom_store_id',  $bl_cols, true ) ) $bl_adds[] = 'ADD COLUMN custom_store_id  VARCHAR(100) NULL DEFAULT NULL';
		if ( ! in_array( 'is_direct_author', $bl_cols, true ) ) $bl_adds[] = 'ADD COLUMN is_direct_author TINYINT(1)   NOT NULL DEFAULT 0';
		if ( ! in_array( 'is_deleted',       $bl_cols, true ) ) $bl_adds[] = 'ADD COLUMN is_deleted       TINYINT(1)   NOT NULL DEFAULT 0';
		if ( ! in_array( 'deleted_at',       $bl_cols, true ) ) $bl_adds[] = 'ADD COLUMN deleted_at       DATETIME     NULL DEFAULT NULL';
		if ( ! in_array( 'created_at',       $bl_cols, true ) ) $bl_adds[] = 'ADD COLUMN created_at       DATETIME     NULL DEFAULT NULL';

		if ( ! empty( $bl_adds ) ) {
			$wpdb->query( "ALTER TABLE {$bl_table} " . implode( ', ', $bl_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
		}

		$cc = $wpdb->get_charset_collate();
		dbDelta( MBS_DB_Schema::contributors( $wpdb->prefix . 'mbs_contributors', $cc ) );
	}
}
