<?php
/**
 * Uninstall — drop tables, delete options and the /returns/ page only when the merchant
 * explicitly opted in via the "Delete data on uninstall" toggle.
 *
 * @package WPNest_Returns_For_WooCommerce
 */

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
// Reason: this file runs ONLY in WP_UNINSTALL_PLUGIN context; no namespace pollution possible.

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

if ( ! get_option( 'wpnest_rfw_delete_data_on_uninstall' ) ) {
    return;
}

global $wpdb;

$wpnest_rfw_requests_table = $wpdb->prefix . 'wpnest_rfw_requests';
$wpnest_rfw_log_table      = $wpdb->prefix . 'wpnest_rfw_status_log';
$wpnest_rfw_items_table    = $wpdb->prefix . 'wpnest_rfw_request_items';

// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom-table cleanup on uninstall.
$wpdb->query( "DROP TABLE IF EXISTS `{$wpnest_rfw_items_table}`" );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom-table cleanup on uninstall.
$wpdb->query( "DROP TABLE IF EXISTS `{$wpnest_rfw_requests_table}`" );
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom-table cleanup on uninstall.
$wpdb->query( "DROP TABLE IF EXISTS `{$wpnest_rfw_log_table}`" );

$wpnest_rfw_options = array(
    'wpnest_rfw_version',
    'wpnest_rfw_db_version',
    'wpnest_rfw_page_id',
    'wpnest_rfw_enabled',
    'wpnest_rfw_withdrawal_window_days',
    'wpnest_rfw_button_label',
    'wpnest_rfw_merchant_email',
    'wpnest_rfw_delete_data_on_uninstall',
    'wpnest_rfw_excluded_statuses',
    'wpnest_rfw_exclude_digital_with_waiver',
    'wpnest_rfw_exclude_customized',
    'wpnest_rfw_exclude_subscriptions',
    'wpnest_rfw_excluded_product_ids',
    'wpnest_rfw_installed_at',
);

$wpnest_rfw_page_id = (int) get_option( 'wpnest_rfw_page_id' );
if ( $wpnest_rfw_page_id > 0 && get_post( $wpnest_rfw_page_id ) ) {
    wp_delete_post( $wpnest_rfw_page_id, true );
}

foreach ( $wpnest_rfw_options as $wpnest_rfw_opt ) {
    delete_option( $wpnest_rfw_opt );
}

// Clear plugin transients (guest tokens + rate-limit counters).
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- One-shot cleanup on uninstall.
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
        $wpdb->esc_like( '_transient_wpnest_rfw_' ) . '%',
        $wpdb->esc_like( '_transient_timeout_wpnest_rfw_' ) . '%'
    )
);

// Remove the confirmation-PDF files from the uploads directory. The empty
// `wpnest-rfw/` folder itself is intentionally left behind — wp.org guidelines
// discourage direct filesystem dir removal in uninstall, and an empty hardened
// directory does no harm.
$wpnest_rfw_upload = wp_upload_dir();
if ( ! empty( $wpnest_rfw_upload['basedir'] ) ) {
    $wpnest_rfw_dir = trailingslashit( $wpnest_rfw_upload['basedir'] ) . 'wpnest-rfw';
    if ( is_dir( $wpnest_rfw_dir ) ) {
        $wpnest_rfw_files = glob( $wpnest_rfw_dir . '/*' );
        if ( is_array( $wpnest_rfw_files ) ) {
            foreach ( $wpnest_rfw_files as $wpnest_rfw_file ) {
                if ( is_file( $wpnest_rfw_file ) ) {
                    wp_delete_file( $wpnest_rfw_file );
                }
            }
        }
    }
}
