<?php

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

/** Delete Content Miner data after the SDK has reported an uninstall event. */
function content_miner_uninstall_cleanup() {
	$content_miner_secret_file = __DIR__ . '/class-content-miner-secret-vault.php';
	if ( file_exists( $content_miner_secret_file ) ) {
		require_once $content_miner_secret_file;
	}

	$content_miner_protected_tokens = get_option( 'content_miner_gsc_tokens', '' );
	$content_miner_vault_class      = 'Content_Miner_' . 'Secret_Vault';
	if ( class_exists( $content_miner_vault_class ) && is_string( $content_miner_protected_tokens ) && '' !== $content_miner_protected_tokens ) {
		$content_miner_vault = new $content_miner_vault_class();
		$content_miner_json  = $content_miner_vault->decrypt( $content_miner_protected_tokens );
		if ( ! is_wp_error( $content_miner_json ) ) {
			$content_miner_tokens = json_decode( $content_miner_json, true );
			if ( is_array( $content_miner_tokens ) && ! empty( $content_miner_tokens['refresh_token'] ) ) {
				wp_remote_post(
					'https://oauth2.googleapis.com/revoke',
					array(
						'timeout' => 5,
						'body'    => array( 'token' => (string) $content_miner_tokens['refresh_token'] ),
					)
				);
			}
		}
	}

	$content_miner_options = array(
		'content_miner_gsc_config',
		'content_miner_gsc_tokens',
		'content_miner_gsc_status',
		'content_miner_ga4_status',
		'content_miner_latest_scan',
		'content_miner_ingestion_state',
		'content_miner_ingestion_lock',
		'content_miner_memory_summary',
		'content_miner_hash_index',
		'content_miner_recent_changes',
		'content_miner_dirty_queue',
		'content_miner_locations',
		'content_miner_openai_key',
		'content_miner_vector_schema',
		'content_miner_analysis_state',
		'content_miner_analysis_lock',
		'content_miner_analysis_handoff',
		'content_miner_vector_backfill_required',
		'content_miner_decision_schema',
		'content_miner_digest_settings',
		'content_miner_digest_state',
		'content_miner_openai_chat_model',
		'content_miner_verification_state',
		'content_miner_verification_lock',
		'content_miner_render_epoch',
		'content_miner_consultation_cta',
		'content_miner_scoring_sitemap',
		'content_miner_free_analysis_usage',
		'content_miner_free_activation',
	);

	foreach ( $content_miner_options as $content_miner_option ) {
		delete_option( $content_miner_option );
	}

	foreach ( array( '_content_miner_memory', '_content_miner_rendered_text', '_content_miner_search_console', '_content_miner_google_analytics', '_content_miner_scoring_override' ) as $content_miner_meta_key ) {
		delete_post_meta_by_key( $content_miner_meta_key );
	}

	foreach ( array( 'content_miner_process_memory_batch', 'content_miner_process_analysis_batch', 'content_miner_gsc_sync_batch', 'content_miner_ga4_sync_batch', 'content_miner_capture_decision_outcomes', 'content_miner_send_monthly_digest', 'content_miner_process_verification_batch', 'content_miner_retry_free_activation' ) as $content_miner_hook ) {
		wp_unschedule_hook( $content_miner_hook );
	}

	global $wpdb;
	$content_miner_vector_table = $wpdb->prefix . 'content_miner_vectors';
	$wpdb->query( "DROP TABLE IF EXISTS {$content_miner_vector_table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$content_miner_decision_table = $wpdb->prefix . 'content_miner_decisions';
	$wpdb->query( "DROP TABLE IF EXISTS {$content_miner_decision_table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
