<?php
/**
 * STRYKE WP — Uninstall Handler.
 *
 * Runs when the plugin is deleted via WordPress admin.
 * Removes ALL plugin data: options, custom tables, transients, cron jobs.
 *
 * Custom tables dropped:
 *   - {prefix}stryke_logs         (Hub Connector audit log)
 *   - {prefix}stryke_backups_log  (Google Drive Backup log)
 *
 * Legacy options also cleaned up for users migrating from individual plugins.
 *
 * NOTE: uninstall.php is self-contained — the autoloader is not available here.
 * Use raw SQL and direct WP functions rather than plugin classes.
 *
 * @package StrykeWP
 */

// Security: only run from WordPress uninstall context.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

global $wpdb;

// ---------------------------------------------------------------------------
// Drop custom DB tables.
// ---------------------------------------------------------------------------

$tables_to_drop = [
	$wpdb->prefix . 'stryke_logs',        // Hub Connector: ApiLogger
	$wpdb->prefix . 'stryke_backups_log', // Backup: BackupSettings
];

foreach ( $tables_to_drop as $table ) {
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$wpdb->query( "DROP TABLE IF EXISTS {$table}" );
}

// ---------------------------------------------------------------------------
// Remove all master-plugin options (prefixed with stryke_wp_).
// ---------------------------------------------------------------------------

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'stryke\_wp\_%'" );

// ---------------------------------------------------------------------------
// Remove legacy individual-plugin options (backward compat migration data).
// ---------------------------------------------------------------------------

$legacy_options = [
	// stryke-connector
	'stryke_hub_api_key_hash',
	'stryke_hub_db_version',
	// stryke-backup
	'stryke_backup_options',
	// stryke-wip
	'stryke_wip_active',
	'stryke_wip_allowed_pages_ids',
];

foreach ( $legacy_options as $option ) {
	delete_option( $option );
}

// ---------------------------------------------------------------------------
// Remove Branding user_meta (admin_color, stryke_theme_mode).
// ---------------------------------------------------------------------------

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( "UPDATE {$wpdb->usermeta} SET meta_value = 'fresh' WHERE meta_key = 'admin_color' AND meta_value = 'stryke-dark'" );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key = 'stryke_theme_mode'" );

// ---------------------------------------------------------------------------
// Remove transients (connector raw-key transient + all stryke_ transients).
// ---------------------------------------------------------------------------

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_stryke\_%'" );
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_timeout\_stryke\_%'" );

// ---------------------------------------------------------------------------
// Clear scheduled cron events.
// ---------------------------------------------------------------------------

wp_clear_scheduled_hook( 'stryke_hub_prune_logs' );
wp_clear_scheduled_hook( 'stryke_scheduled_backup_event' );

// ---------------------------------------------------------------------------
// Multisite: clean up each site's options and tables.
// ---------------------------------------------------------------------------

if ( is_multisite() ) {
	$sites = get_sites( [ 'number' => 0, 'fields' => 'ids' ] );

	foreach ( $sites as $site_id ) {
		switch_to_blog( $site_id );

		// Build table names AFTER switch_to_blog() so $wpdb->prefix is correct for this site.
		$site_tables = [
			$wpdb->prefix . 'stryke_logs',
			$wpdb->prefix . 'stryke_backups_log',
		];

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE 'stryke\_wp\_%'" );

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_stryke\_%'" );
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_timeout\_stryke\_%'" );

		foreach ( $legacy_options as $option ) {
			delete_option( $option );
		}

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( "UPDATE {$wpdb->usermeta} SET meta_value = 'fresh' WHERE meta_key = 'admin_color' AND meta_value = 'stryke-dark'" );
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared
		$wpdb->query( "DELETE FROM {$wpdb->usermeta} WHERE meta_key = 'stryke_theme_mode'" );

		foreach ( $site_tables as $table ) {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			$wpdb->query( "DROP TABLE IF EXISTS {$table}" );
		}

		wp_clear_scheduled_hook( 'stryke_hub_prune_logs' );
		wp_clear_scheduled_hook( 'stryke_scheduled_backup_event' );

		restore_current_blog();
	}
}
