<?php
/**
 * File Path: includes/migrations/class-mbs-migration-4-17.php
 *
 * Migration 4.17 — Retire mbs_collection_{term_id}_position postmeta keys
 *
 * Deletes all wp_postmeta rows whose meta_key matches the dynamic pattern
 * mbs_collection_%_position. These keys were the original per-collection
 * position storage. Migration 4.12 backfilled them into the relational
 * mbs_book_collection_positions table; the render and save paths were updated
 * in Session 162 to use MBS_Book_Collection_Position_DAO exclusively.
 *
 * Idempotent: DELETE WHERE LIKE is safe to replay (no rows = no-op).
 *
 * @package MyBookShowroom
 * @since   1.7.0
 */

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

/**
 * Migration 4.17: retire dynamic collection-position postmeta keys.
 */
class MBS_Migration_4_17 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.17';
	}

	/**
	 * Run the migration.
	 *
	 * Deletes all wp_postmeta rows whose meta_key matches
	 * mbs_collection_%_position. The mbs_book_collection_positions table
	 * is the canonical source after migration 4.12 + Session 162 render fix.
	 *
	 * @return void
	 */
	public function up(): void {
		global $wpdb;

		// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- DAO layer -- direct DB access is intentional per MBS architecture
			$wpdb->prepare(
				"DELETE FROM {$wpdb->postmeta}
				 WHERE meta_key LIKE %s",
				$wpdb->esc_like( 'mbs_collection_' ) . '%' . $wpdb->esc_like( '_position' )
			)
		);
	}
}
