<?php
/**
 * Nettoyage Volade SDK à la désinstallation du plugin hôte.
 *
 * @package Volade\SDK
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

/**
 * Supprime tables, options, transients et tâches planifiées Volade.
 */
if ( ! function_exists( 'volade_sdk_uninstall_cleanup' ) ) {
	function volade_sdk_uninstall_cleanup() {
		global $wpdb;

		$tables = array(
			'volade_mission_completions',
			'volade_reviews',
			'volade_sites',
			'volade_subscriptions',
			'volade_events',
			'volade_referrals',
			'volade_badges',
			'volade_tokens',
			'volade_users',
		);

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

		$options = array(
			'volade_sdk_db_version',
			'volade_sdk_active_count',
			'volade_show_badge',
			'volade_show_social_proof',
			'volade_show_guest_banner',
			'volade_show_admin_footer',
			'volade_api_url',
			'volade_api_key',
		);

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

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

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

		wp_clear_scheduled_hook( 'volade_daily_cron' );
	}
}
