<?php

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

global $wpdb;

if ( 'no' === get_option( 'zacls_delete_data_on_uninstall', 'yes' ) ) {
	return;
}

// 1. Drop all custom tables.
$tables = array(
	"{$wpdb->prefix}zacls_login_attempts",
	"{$wpdb->prefix}zacls_blocked_ips",
	"{$wpdb->prefix}zacls_2fa_codes",
	"{$wpdb->prefix}zacls_2fa_status",
	"{$wpdb->prefix}zacls_sessions",
	"{$wpdb->prefix}zacls_device_fingerprints",
	"{$wpdb->prefix}zacls_lockdown",
	"{$wpdb->prefix}zacls_audit_log",
	"{$wpdb->prefix}zacls_webauthn_credentials",
	"{$wpdb->prefix}zacls_webauthn_challenges",
	"{$wpdb->prefix}zacls_password_history",
	"{$wpdb->prefix}zacls_risk_scores",
);

foreach ( $tables as $table ) {
	$safe_table = esc_sql( $table );
	$wpdb->query( "DROP TABLE IF EXISTS {$safe_table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
}

// 2. Delete all plugin options.
$options = array(
	// Core
	'zacls_2fa_enabled',
	'zacls_2fa_forced_roles',
	'zacls_2fa_methods',
	'zacls_login_limits',
	'zacls_auto_block_threshold',
	'zacls_auto_block_duration',
	'zacls_custom_login_slug',
	'zacls_block_wp_login',
	'zacls_redirect_unauthorized',
	'zacls_email_notifications',
	'zacls_admin_email',
	'zacls_onboarding_completed',
	'zacls_db_version',
	'zacls_flush_rewrite',
	'zacls_password_policy',
	'zacls_emergency_lockdown',
	'zacls_lockdown_whitelist',
	'zacls_session_timeout',
	'zacls_session_inactive_limit',
	'zacls_device_tracking',
	'zacls_cloudflare_enabled',
	'zacls_trusted_proxy_ips',
	'zacls_geoip_enabled',
	'zacls_tor_detection',
	'zacls_report_frequency',
	'zacls_last_report_sent',
	'zacls_delete_data_on_uninstall',

	// File Monitor & Vulnerability
	'zacls_file_check_last',
	'zacls_file_scan_results',
	'zacls_file_scan_time',
	'zacls_vuln_scan_last',
	'zacls_vuln_scan_results',
	'zacls_wpscan_api_token',

	// WebAuthn
	'zacls_webauthn_enabled',
	'zacls_webauthn_bypasses_2fa',

	// Branding
	'zacls_login_logo_image',
	'zacls_login_logo_url',
	'zacls_login_logo_title',
	'zacls_login_bg_color',
	'zacls_login_form_bg',
	'zacls_login_text_color',
	'zacls_login_primary_color',
	'zacls_login_custom_css',
	'zacls_login_header_html',
	'zacls_login_footer_html',

	// Wizard
	'zacls_wizard_applied',

	// Background Queue
	'zacls_background_queue',

	// Auto Cleanup
	'zacls_cleanup_frequency',
	'zacls_cleanup_retention_days',

	// Common Passwords
	'zacls_common_passwords',

	// Weak Password Flag
	'zacls_weak_password',
);

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

// 3. Delete all plugin transients.
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
	"DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_zacls_%' OR option_name LIKE '_transient_timeout_zacls_%'"
);

// 4. Clear all scheduled cron jobs.
$cron_hooks = array(
	'zacls_daily_cleanup',
	'zacls_process_queue',
	'zacls_daily_report',
	'zacls_weekly_report',
	'zacls_monthly_report',
);

foreach ( $cron_hooks as $hook ) {
	$timestamp = wp_next_scheduled( $hook );
	if ( $timestamp ) {
		wp_unschedule_event( $timestamp, $hook );
	}
}

// 5. Delete user metadata.
$user_meta_keys = array(
	'zacls_known_ips',
	'zacls_notifications',
	'zacls_2fa_secret',
	'zacls_last_country',
	'zacls_webauthn_credentials',
);

foreach ( $user_meta_keys as $meta_key ) {
	$wpdb->delete( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.SlowDBQuery.slow_db_query_meta_key
		$wpdb->usermeta,
		array( 'meta_key' => $meta_key ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
		array( '%s' )
	);
}
