<?php
// If this file is called directly, abort.
if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}

/**
 * Delete plugin options.
 *
 * @param int|null $site_id Optional. Site ID for multisite, defaults to null.
 */
function iafwzdesk_delete_options($site_id = null)
{
    $options = [
        'iafwzdesk_delete_data_on_uninstall',
        'iafwzdesk_error_notification',
        'iafwzdesk_error_notification_email',
        'iafwzdesk_error_notification_subject',
        'iafwzdesk_access_tokens',
        'iafwzdesk_session_key',
        'iafwzdesk_session_plan',
        'iafwzdesk_request_key',
        'iafwzdesk_review_enabled',
        'iafwzdesk_review_dismissed',
        'iafwzdesk_review_remind_me_later',
        'iafwzdesk_spam_detection_keywords',
        'iafwzdesk_spam_detection_handling',
        'iafwzdesk_email_detection_keywords',
        'iafwzdesk_email_detection_handling',
        'iafwzdesk_email_validation_keywords',
        'iafwzdesk_email_validation_handling'
    ];

    // Switch to the specific site if a site ID is provided (multisite support)
    if ($site_id !== null && is_multisite()) {
        switch_to_blog($site_id);
    }

    foreach ($options as $option) {
        delete_option($option); // Delete each option
    }

    // Restore the current site if a site ID was provided
    if ($site_id !== null && is_multisite()) {
        restore_current_blog();
    }
}

/**
 * Delete plugin tables.
 *
 * @param int|null $site_id Optional. Site ID for multisite, defaults to null.
 */
function iafwzdesk_delete_table($site_id = null)
{
    global $wpdb;

    $table_list = [
        'iafwzdesk_error_log',
        'iafwzdesk_field_mapping',
        'iafwzdesk_account_details',
    ];

    // Switch to the specific site if a site ID is provided (multisite support)
    if ($site_id !== null && is_multisite()) {
        switch_to_blog($site_id);
    }

    foreach ($table_list as $table) {
        $full_table_name = $wpdb->prefix . $table;

        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange
        $wpdb->query("DROP TABLE IF EXISTS `" . esc_sql($full_table_name) . "`");
    }

    // Restore the current site if a site ID was provided
    if ($site_id !== null && is_multisite()) {
        restore_current_blog();
    }
}

/**
 * Main uninstallation process.
 */
function iafwzdesk_run_uninstall()
{
    if (is_multisite()) {
        // Handle network-wide uninstall
        $sites = get_sites(['fields' => 'ids']);
        foreach ($sites as $site_id) {
            // Switch to each site
            switch_to_blog($site_id);

            // Check if data deletion is enabled
            $delete_status = get_option('iafwzdesk_delete_data_on_uninstall', '');
            if ($delete_status === 'yes') {
                iafwzdesk_delete_options($site_id);
                iafwzdesk_delete_table($site_id);
            }

            // Restore the original site context
            restore_current_blog();
        }
    } else {
        // Handle single-site uninstall
        $delete_status = get_option('iafwzdesk_delete_data_on_uninstall', '');
        if ($delete_status === 'yes') {
            iafwzdesk_delete_options();
            iafwzdesk_delete_table();
        }
    }
}

// Execute the uninstallation process
iafwzdesk_run_uninstall();
