<?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 iafwzcai_delete_options($site_id = null)
{
    $options = [
        'iafwzcai_delete_data_on_uninstall',
        'iafwzcai_error_notification',
        'iafwzcai_error_notification_email',
        'iafwzcai_error_notification_subject',
        'iafwzcai_access_tokens',
        'iafwzcai_session_key',
        'iafwzcai_session_plan',
        'iafwzcai_request_key',
        'iafwzcai_review_enabled',
        'iafwzcai_review_dismissed',
        'iafwzcai_review_remind_me_later',
        'iafwzcai_spam_detection_keywords',
        'iafwzcai_spam_detection_handling',
        'iafwzcai_email_detection_keywords',
        'iafwzcai_email_detection_handling',
        'iafwzcai_email_validation_keywords',
        'iafwzcai_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 iafwzcai_delete_table($site_id = null)
{
    global $wpdb;

    $table_list = [
        'iafwzcai_error_log',
        'iafwzcai_field_mapping',
        'iafwzcai_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 `{$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 iafwzcai_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('iafwzcai_delete_data_on_uninstall', '');
            if ($delete_status === 'yes') {
                iafwzcai_delete_options($site_id);
                iafwzcai_delete_table($site_id);
            }

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

// Execute the uninstallation process
iafwzcai_run_uninstall();
