<?php
/**
 * Uninstall cleanup shared by uninstall.php and the Freemius after_uninstall hook.
 *
 * @package Carticy\Conversions\Core
 */

declare(strict_types=1);

namespace Carticy\Conversions\Core;

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

/**
 * Removes every trace of the plugin when the merchant opted in to data deletion.
 */
final class Uninstaller {

	private const OPTIONS = array(
		'carticy_conversions_enabled',
		'carticy_conversions_activated_at',
		'carticy_conversions_db_version',
		'carticy_conversions_consent_posture',
		'carticy_conversions_consent_attestation',
		'carticy_conversions_ip_mode',
		'carticy_conversions_retention_days',
		'carticy_conversions_client_tracking',
		'carticy_conversions_server_tracking',
		'carticy_conversions_debug_log',
	);

	private const CRON_HOOKS = array(
		'carticy_conversions_retention_purge',
		'carticy_conversions_process_server_dispatch_queue',
		'carticy_conversions_server_dispatch_sweep',
	);

	/**
	 * Run the full cleanup when the merchant opted in.
	 */
	public static function cleanup(): void {
		if ( 'yes' !== get_option( 'carticy_conversions_delete_data_on_uninstall', 'no' ) ) {
			return;
		}

		global $wpdb;

		foreach ( self::CRON_HOOKS as $hook ) {
			if ( function_exists( 'wp_clear_scheduled_hook' ) ) {
				wp_clear_scheduled_hook( $hook );
			}
		}

		foreach ( self::OPTIONS as $option ) {
			delete_option( $option );
		}

		// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Uninstall must remove plugin-owned data directly; the trusted prefixed table name is interpolated and values are prepared.
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
				$wpdb->esc_like( '_transient_carticy_conversions_' ) . '%',
				$wpdb->esc_like( '_transient_timeout_carticy_conversions_' ) . '%'
			)
		);

		// Order meta for both storage backends: HPOS orders_meta and postmeta.
		$meta_like = $wpdb->esc_like( '_carticy_conversions_' ) . '%';
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s", $meta_like ) );
		$hpos_meta_table = $wpdb->prefix . 'wc_orders_meta';
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is built from the trusted prefix.
		if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $hpos_meta_table ) ) === $hpos_meta_table ) {
			$wpdb->query( $wpdb->prepare( "DELETE FROM {$hpos_meta_table} WHERE meta_key LIKE %s", $meta_like ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		}

		// The dispatch-claim table.
		$dispatch_table = $wpdb->prefix . 'carticy_conversion_dispatches';
		$wpdb->query( "DROP TABLE IF EXISTS {$dispatch_table}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		// phpcs:enable

		delete_option( 'carticy_conversions_delete_data_on_uninstall' );
	}
}
