<?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 iafwzc_delete_options($site_id = null)
{
    $options = [
        'iafwzc_delete_data_on_uninstall',
        'iafwzc_error_notification',
        'iafwzc_error_notification_email',
        'iafwzc_error_notification_subject',
        'iafwzc_access_tokens',
        'iafwzc_session_key',
        'iafwzc_session_plan',
        'iafwzc_request_key',
        'iafwzc_review_enabled',
        'iafwzc_review_dismissed',
        'iafwzc_review_remind_me_later',
        'iafwzc_deactivate_notification',
        'iafwzc_deactivate_notification_email'
    ];

    // 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 iafwzc_delete_table($site_id = null)
{
    global $wpdb;

    $table_list = [
        'iafwzc_error_log',
        'iafwzc_field_mapping',
        'iafwzc_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 iafwzc_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('iafwzc_delete_data_on_uninstall', '');
            if ($delete_status === 'yes') {
                iafwzc_delete_options($site_id);
                iafwzc_delete_table($site_id);
            }

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

// Execute the uninstallation process
iafwzc_run_uninstall();
