<?php
/**
 * File Path: includes/migrations/class-mbs-migration-4-35.php
 *
 * Migration 4.35 -- Sprint RO-Display: mbs_reading_order type retirement.
 *
 * Retires the 'series' and 'collection' type rows from mbs_reading_order
 * (superseded by mbs_book_meta.series_reading_order_position and
 * mbs_book_collection_positions respectively) and removes the corresponding
 * term_meta stubs that were used to flag saved orders.
 *
 * CRITICAL ORDER OF OPERATIONS (B-001):
 *   Step A -- Delete term_meta 'mbs_series_reading_order' for all terms.
 *   Step B -- DELETE FROM mbs_reading_order WHERE type = 'series'. (Idempotent.)
 *   Step C -- Delete term_meta 'mbs_collection_reading_order' for all terms.
 *   Step D -- DELETE FROM mbs_reading_order WHERE type = 'collection'. (Idempotent.)
 *   (mbs_universe_reading_order term_meta rows are RETAINED.)
 *
 * All steps are idempotent: safe to run on a database where the rows have
 * already been removed.
 *
 * @package MyBookShowroom
 * @since   4.35.0
 */

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

class MBS_Migration_4_35 implements MBS_Migration { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Returns the migration version string.
	 *
	 * @return string
	 */
	public function version(): string { return '4.35'; }

	/**
	 * Run the migration.
	 *
	 * @return void
	 */
	public function up(): void {
		global $wpdb;

		$ro_table = $wpdb->prefix . 'mbs_reading_order';

		// ---- Step A: Delete term_meta 'mbs_series_reading_order' ----
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
		$wpdb->delete( $wpdb->termmeta, array( 'meta_key' => 'mbs_series_reading_order' ), array( '%s' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.DirectDatabaseQuery.DirectQuery -- DAO layer -- direct DB access is intentional per MBS architecture

		// ---- Step B: Delete mbs_reading_order rows where type = 'series' ----
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
		$wpdb->delete( $ro_table, array( 'type' => 'series' ), array( '%s' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery -- DAO layer -- direct DB access is intentional per MBS architecture

		// ---- Step C: Delete term_meta 'mbs_collection_reading_order' ----
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
		$wpdb->delete( $wpdb->termmeta, array( 'meta_key' => 'mbs_collection_reading_order' ), array( '%s' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.DirectDatabaseQuery.DirectQuery -- DAO layer -- direct DB access is intentional per MBS architecture

		// ---- Step D: Delete mbs_reading_order rows where type = 'collection' ----
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
		$wpdb->delete( $ro_table, array( 'type' => 'collection' ), array( '%s' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery -- DAO layer -- direct DB access is intentional per MBS architecture
	}
}
