<?php
/**
 * Uninstall AmbassadorHub
 *
 * Runs when the plugin is deleted via the WordPress admin. Removes all plugin
 * data: options, custom database tables, the ambassador user role, and any
 * pending scheduled actions.
 *
 * @package AmbassadorHub
 */

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

global $wpdb;

// ---------------------------------------------------------------------------
// Options
// ---------------------------------------------------------------------------

$ambh_options = array(
	// Core / plugin version
	'ambassador_hub_version',
	'ambassador_hub_activate',
	'ambassador_hub_general_platform_name',
	'ambassador_hub_tools_debug_mode',

	// DB schema version tracking (migration runners)
	'ambh_accounts_db_version',
	'ambh_consent_db_version',
	'ambh_donations_db_version',
	'ambh_donors_db_version',

	// Transient index versioning (cache invalidation)
	'ambh_index_version',

	// Pages
	'ambh_account_page_id',
	'ambh_create_fundraiser_page_id',
	'ambh_login_page_id',
	'ambh_psp_redirect_page_id',

	// Fundraiser settings
	'ambh_fundraiser_min_target',
	'ambh_fundraisers_min_donation',
	'ambh_fundraisers_max_donation',

	// Payment connectors
	'ambh_active_connector_psp',

	// Payment connectors — PayPal
	'ambh_paypal_client_id',
	'ambh_paypal_client_secret',
	'ambh_paypal_mode',

	// Payment connectors — Stripe
	'ambh_stripe_mode',
	'ambh_stripe_test_secret_key',
	'ambh_stripe_live_secret_key',
	'ambh_stripe_webhook_secret',

	// Consent
	'ambh_ambassador_consent_enabled',
	'ambh_ambassador_general_consent_text',
	'ambh_donor_consent_enabled',
	'ambh_donor_general_consent_text',
	'ambh_privacy_policy_consent_text',

	// Admin UI state
	'ambh_checklist_collapsed',
	'ambh_wizard_step_index',
	'ambh_step_newsletter_skipped',
);

foreach ( $ambh_options as $ambh_option ) {
	delete_option( $ambh_option );
}

// Delete any dynamic wizard step options (ambh_step_{key}_skipped).
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'ambh_step_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching

// Delete transient index version options for cache groups.
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", 'ambh_index_version%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching

// ---------------------------------------------------------------------------
// Custom database tables
// ---------------------------------------------------------------------------

$ambh_tables = array(
	$wpdb->prefix . 'ambh_donors',
	$wpdb->prefix . 'ambh_donations',
	$wpdb->prefix . 'ambh_donation_meta',
	$wpdb->prefix . 'ambh_consent_records',
);

foreach ( $ambh_tables as $ambh_table ) {
	$wpdb->query( "DROP TABLE IF EXISTS `{$ambh_table}`" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange -- $ambh_table is built from $wpdb->prefix (safe); DROP required for uninstall.
}

// ---------------------------------------------------------------------------
// User role
// ---------------------------------------------------------------------------

remove_role( 'ambassador' );

// ---------------------------------------------------------------------------
// Scheduled actions (Action Scheduler)
// ---------------------------------------------------------------------------

if ( function_exists( 'as_unschedule_all_actions' ) ) {
	$ambh_hooks = array(
		'ambh_end_fundraiser',
		'ambh_schedule_rule_instances',
	);

	foreach ( $ambh_hooks as $ambh_hook ) {
		as_unschedule_all_actions( $ambh_hook );
	}
}

// ---------------------------------------------------------------------------
// Transients
// ---------------------------------------------------------------------------

// Delete transients
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s", '_transient_ambh_%', '_transient_timeout_ambh_%' ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
