<?php
/**
 * File Path: includes/migrations/class-mbs-migration-4-14.php
 *
 * Migration 4.14 — Drop legacy mbs_contributors table
 *
 * The mbs_contributors table was the original flat contributor storage.
 * It was superseded in Sprint F-020 (DB version 2.4) by:
 *   - mbs_contributor taxonomy (WP terms)
 *   - mbs_book_contributors pivot table
 *
 * Migration 2.4 migrated existing rows and dropped the old table
 * conditionally (only when row counts matched). This migration provides
 * an unconditional DROP TABLE IF EXISTS to clean up any installations
 * where the conditional drop in 2.4 did not fire.
 *
 * MBS_DB_Installer::install() previously called dbDelta() to create
 * mbs_contributors on every fresh activation (removed in this phase).
 * Without that removal, fresh installs created the table unnecessarily
 * before migration 2.4 immediately dropped it again.
 *
 * The MBS_DB_Schema::contributors() method is retained because
 * class-mbs-migration-1-7.php still references it to create the table
 * before migration 2.4 migrates and drops it.
 *
 * @package MyBookShowroom
 * @since   1.7.0
 */

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

/**
 * Migration 4.14: Drop legacy mbs_contributors table.
 */
// 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_4_14 implements MBS_Migration { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * @return string Migration version string.
	 */
	public function version(): string {
		return '4.14';
	}

	/**
	 * Run the migration.
	 *
	 * Unconditionally drops mbs_contributors if it still exists.
	 * Idempotent: DROP TABLE IF EXISTS is a no-op on a missing table.
	 *
	 * @return void
	 */
	public function up(): void {
		global $wpdb;
		$table = $wpdb->prefix . 'mbs_contributors';
		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$wpdb->query( "DROP TABLE IF EXISTS {$table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- DAO layer -- direct DB access is intentional per MBS architecture
	}
}
