<?php
/**
 * File Path: includes/migrations/class-mbs-migration-3-5.php
 *
 * MBS_Migration_3_5 -- Sprint POS-3 -- Add mbs_pos_locations + mbs_pos_inventory
 *
 * Creates two tables for multi-location POS inventory management (OpenPOS):
 *
 *   mbs_pos_locations  - named physical locations (conventions, signings, pop-ups).
 *   mbs_pos_inventory  - per-location, per-format-row stock count. UNIQUE KEY on
 *                        (location_id, format_row_id) enforces one row per pair.
 *
 * Both tables are created via dbDelta (idempotent; safe to re-run).
 * Location UI is PUB-tier gated in the admin; the tables are always present.
 *
 * @package MyBookShowroom
 * @since   3.5.0
 */

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

/**
 * Class MBS_Migration_3_5
 *
 * @implements MBS_Migration
 */
class MBS_Migration_3_5 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 '3.5';
	}

	public function up(): void {
		global $wpdb;
		$charset_collate = $wpdb->get_charset_collate();

		require_once ABSPATH . 'wp-admin/includes/upgrade.php';

		$loc_table = $wpdb->prefix . 'mbs_pos_locations';
		dbDelta( "CREATE TABLE {$loc_table} (
			id         BIGINT UNSIGNED  NOT NULL AUTO_INCREMENT,
			name       VARCHAR(255)     NOT NULL,
			address    VARCHAR(500)     NULL DEFAULT NULL,
			is_active  TINYINT(1)       NOT NULL DEFAULT 1,
			created_at DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			KEY idx_is_active (is_active)
		) {$charset_collate};" );

		$inv_table = $wpdb->prefix . 'mbs_pos_inventory';
		dbDelta( "CREATE TABLE {$inv_table} (
			id             BIGINT UNSIGNED  NOT NULL AUTO_INCREMENT,
			location_id    BIGINT UNSIGNED  NOT NULL,
			format_row_id  BIGINT UNSIGNED  NOT NULL,
			stock_qty      INT UNSIGNED     NOT NULL DEFAULT 0,
			updated_at     DATETIME         NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
			PRIMARY KEY (id),
			UNIQUE KEY idx_loc_format (location_id, format_row_id),
			KEY idx_location_id (location_id),
			KEY idx_format_row_id (format_row_id)
		) {$charset_collate};" );
	}
}
