<?php
/**
 * Uninstall handler. Only runs when the merchant explicitly clicks
 * "Delete" on the Plugins screen - deactivation alone does not fire this.
 * Cleans up every option and custom table the plugin created so no
 * orphaned rows are left in the database.
 */

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

global $wpdb;

// --------------------------------------------------------------------
// Plugin options (mirrors the keys written by ConfigPage + WizardPage).
// --------------------------------------------------------------------
$pay4all_for_mypos_options = [
    'pay4all_for_mypos_mode',
    'pay4all_for_mypos_pack_test',
    'pay4all_for_mypos_pack_live',
    'pay4all_for_mypos_methods_enabled',
    'pay4all_for_mypos_status_non_virtual',
    'pay4all_for_mypos_status_virtual',
    'pay4all_for_mypos_status_deferred',
    'pay4all_for_mypos_status_failed',
    'pay4all_for_mypos_status_cancelled',
    'pay4all_for_mypos_wizard_complete',
    'pay4all_for_mypos_prod_deferred',
    'pay4all_for_mypos_delete_logs_after',
    'pay4all_for_mypos_db_version',
];
foreach ( $pay4all_for_mypos_options as $pay4all_for_mypos_opt ) {
    delete_option( $pay4all_for_mypos_opt );
}

// --------------------------------------------------------------------
// Custom table (pending payments tracker).
//
// The table name is built from a hard-coded suffix and $wpdb->prefix
// (which the DB layer trusts as safe). $wpdb->prepare() rejects
// placeholders in a table identifier - the SQL below is safe because
// $pay4all_for_mypos_table can only take a single, plugin-controlled
// value. Caching is irrelevant at uninstall time.
// --------------------------------------------------------------------
$pay4all_for_mypos_table = $wpdb->prefix . 'pay4all_for_mypos_pending_payments';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- uninstall-time DROP of a plugin-owned table ; identifier can't be prepared.
$wpdb->query( "DROP TABLE IF EXISTS `{$pay4all_for_mypos_table}`" );

// --------------------------------------------------------------------
// Scheduled cron events + transients.
// --------------------------------------------------------------------
wp_clear_scheduled_hook( 'pay4all_for_mypos_check_order_statuses' );
delete_transient( 'pay4all_for_mypos_wizard_flash' );
