<?php
// If uninstall not called from WordPress, then exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

// Delete all plugin options.
$options_to_delete = array(
	'x4email_settings',
	'x4email_license_key',
	'x4email_license_status',
	'x4email_smtp_settings',
	'x4email_email_templates',
	'x4email_admin_settings',
	'x4email_version',
	'x4email_activation_date',
	'x4email_last_update_check',
	'x4email_notification_settings',
	'x4email_debug_mode',
	'x4email_custom_styles',
	'x4email_custom_scripts',
	'x4email_woocommerce_settings',
);

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

// Delete all plugin transients using WordPress transient functions where possible.
// For pattern-based transient deletion, we need direct queries as WordPress doesn't provide this functionality.
global $wpdb;

// Alternative approach: Use WordPress transient functions for known transients.
$known_transients = array(
	'x4email_smtp_test',
	'x4email_license_check',
	'x4email_update_check',
	'x4email_dashboard_stats',
);

foreach ( $known_transients as $transient ) {
	delete_transient( $transient );
}

// For unknown/dynamic transients, we still need direct query with proper preparation.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- No WordPress function exists for pattern-based transient deletion, uninstall cleanup.
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
		$wpdb->esc_like( '_transient_x4email_' ) . '%'
	)
);

// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- No WordPress function exists for pattern-based transient deletion, uninstall cleanup.
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
		$wpdb->esc_like( '_transient_timeout_x4email_' ) . '%'
	)
);

// Drop custom tables using proper escaping.
$table_name = $wpdb->prefix . 'x4email_logs_lite';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange -- WordPress provides no function for dropping custom tables, required for complete uninstall.
$wpdb->query( 'DROP TABLE IF EXISTS `' . esc_sql( $table_name ) . '`' );

// Initialize WP_Filesystem.
if ( ! function_exists( 'WP_Filesystem' ) ) {
	require_once ABSPATH . 'wp-admin/includes/file.php';
}
WP_Filesystem();
global $wp_filesystem;

// Delete custom email templates.
$templates_dir = plugin_dir_path( __FILE__ ) . 'includes/templates/';

if ( is_dir( $templates_dir ) ) {
	$files = glob( $templates_dir . '*' );
	foreach ( $files as $file ) {
		if ( is_file( $file ) ) {
			$wp_filesystem->delete( $file );
		}
	}
	$wp_filesystem->delete( $templates_dir, true );
}

// Delete custom templates from uploads directory.
$upload_dir           = wp_upload_dir();
$custom_templates_dir = $upload_dir['basedir'] . '/x4email_lt_tmplt';
if ( is_dir( $custom_templates_dir ) ) {
	$files = glob( $custom_templates_dir . '/*' );
	foreach ( $files as $file ) {
		if ( is_file( $file ) ) {
			$wp_filesystem->delete( $file );
		}
	}
	$wp_filesystem->delete( $custom_templates_dir, true );
}

// Delete any custom post types created by the plugin.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- WordPress provides no function for bulk post type deletion, required for complete uninstall.
$wpdb->query(
	$wpdb->prepare(
		"DELETE FROM {$wpdb->posts} WHERE post_type LIKE %s",
		$wpdb->esc_like( 'x4email_' ) . '%'
	)
);

// Clean up orphaned postmeta using a proper subquery.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- WordPress provides no function for orphaned metadata cleanup, required for database integrity.
$wpdb->query(
	"DELETE pm FROM {$wpdb->postmeta} pm 
     LEFT JOIN {$wpdb->posts} p ON pm.post_id = p.ID 
     WHERE p.ID IS NULL"
);

// Clear any cached data that has been removed.
wp_cache_flush();
