<?php
/**
 * File Path: includes/migrations/class-mbs-migration-4-31.php
 *
 * Migration 4.31 -- Session 187: UX-EDIT-002 follow-up -- Reset global default
 * section order to the canonical order defined in MBS_Section_Helper::get_canonical_keys().
 *
 * Migration 4.30 called seed_default_section_order() which is append-only: it
 * skips rows that already exist, preserving stale sort_order values on pre-existing
 * keys. This left the global default in an incorrect display order (e.g. a section
 * added early in the plugin's history sitting at sort_order=1 even though it belongs
 * later in the canonical sequence).
 *
 * MBS_Section_Order_DAO::save_order() uses INSERT ... ON DUPLICATE KEY UPDATE,
 * which atomically resets sort_order for existing rows AND deletes any rows no
 * longer in the canonical list. A single call to save_order(0, canonical_keys)
 * is therefore the correct idempotent remedy.
 *
 * @package MyBookShowroom
 * @since   1.5.0
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class MBS_Migration_4_31 implements MBS_Migration { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix
	public function version(): string { return '4.31'; }
	public function up(): void {
		if ( class_exists( 'MBS_Section_Order_DAO' ) && class_exists( 'MBS_Section_Helper' ) ) {
			MBS_Section_Order_DAO::save_order( 0, MBS_Section_Helper::get_canonical_keys() );
		}
	}
}
