<?php
/**
 * Freemius-compatible uninstall cleanup.
 *
 * @package CertaCart_EU_Product_Readiness
 */

namespace CertaCart\EU_Product_Readiness;

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange -- Explicit data deletion must remove the plugin's dedicated tables.

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

final class Uninstaller {
	public static function uninstall(): void {
		if ( is_multisite() ) {
			$site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0 ) );
			foreach ( $site_ids as $site_id ) {
				switch_to_blog( (int) $site_id );
				self::uninstall_site();
				restore_current_blog();
			}
			return;
		}
		self::uninstall_site();
	}

	/**
	 * Delete plugin data for one site only when the merchant explicitly opted in.
	 */
	public static function uninstall_site(): void {
		global $wpdb;
		$settings = get_option( Settings::OPTION, array() );
		if ( ! is_array( $settings ) || 'yes' !== ( $settings['delete_data_on_uninstall'] ?? 'no' ) ) {
			return;
		}

		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}ccpr_product_results" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}ccpr_scans" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared

		$meta_keys = array(
			'_ccpr_targets_eu', '_ccpr_display_information', '_ccpr_manufacturer_name', '_ccpr_manufacturer_address',
			'_ccpr_manufacturer_email', '_ccpr_manufacturer_url', '_ccpr_manufacturer_in_eu', '_ccpr_responsible_person_name',
			'_ccpr_responsible_person_address', '_ccpr_responsible_person_email', '_ccpr_responsible_person_url',
			'_ccpr_product_identifier', '_ccpr_safety_state', '_ccpr_safety_warning', '_ccpr_safety_information_url',
			'_ccpr_internal_evidence_note',
		);
		foreach ( $meta_keys as $meta_key ) {
			delete_post_meta_by_key( $meta_key );
		}

		$profiles = get_posts(
			array(
				'post_type'      => 'ccpr_profile',
				'post_status'    => 'any',
				'posts_per_page' => -1,
				'fields'         => 'ids',
				'no_found_rows'  => true,
			)
		);
		foreach ( $profiles as $profile_id ) {
			wp_delete_post( (int) $profile_id, true );
		}
		delete_post_meta_by_key( '_ccpr_pro_manufacturer_profile' );
		delete_post_meta_by_key( '_ccpr_pro_rp_profile' );
		delete_option( Settings::OPTION );
		delete_option( 'ccpr_pro_settings' );
		delete_option( 'ccpr_pro_active_scheduled_scan' );
		delete_option( 'ccpr_pro_bulk_operation_log' );
		foreach (
			array(
				'ccpr_pro_bulk_rollback_',
				'_transient_ccpr_pro_tools_notice_',
				'_transient_timeout_ccpr_pro_tools_notice_',
				'_transient_ccpr_pro_tools_preview_',
				'_transient_timeout_ccpr_pro_tools_preview_',
			) as $option_prefix
		) {
			$premium_option_pattern = $wpdb->esc_like( $option_prefix ) . '%';
			$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $premium_option_pattern ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		}
		delete_option( 'ccpr_version' );
		delete_option( 'ccpr_schema_version' );
		delete_option( 'ccpr_scan_start_lock' );
		$lock_pattern = $wpdb->esc_like( 'ccpr_scan_lock_' ) . '%';
		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", $lock_pattern ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		delete_transient( 'ccpr_activation_redirect' );
		wp_clear_scheduled_hook( Cleanup::HOOK );
		wp_clear_scheduled_hook( 'ccpr_pro_scheduled_scan' );
		wp_clear_scheduled_hook( 'ccpr_pro_process_scan' );
	}
}
