<?php
/**
 * Uninstall handler for Vareon Calculator Suite Lite
 *
 * Removes all plugin data when the plugin is deleted via WordPress admin.
 * This file is called automatically by WordPress during plugin uninstallation.
 *
 * @package Vareon_Calculator_Suite_Lite
 */

// Exit if not called by WordPress uninstall process.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

/**
 * Clean up all plugin data for a single site.
 *
 * @return void
 */
function vareoncalc_lite_cleanup_site_data() {
	// Delete plugin options.
	delete_option( 'vareoncalc_lite_options' );
	delete_option( 'vareoncalc_onboarding_status' );
	delete_option( 'vareoncalc_review_cta_state' );

	// Delete transients.
	delete_transient( 'vareoncalc_activation_redirect' );
	delete_transient( 'vareoncalc_pro_activated' );
	delete_transient( 'vareoncalc_pro_deactivated' );
}

/**
 * Clean up user meta for all users.
 *
 * @return void
 */
function vareoncalc_lite_cleanup_user_meta() {
	delete_metadata( 'user', 0, 'vareoncalc_upgrade_notice_dismissed', '', true );
	delete_metadata( 'user', 0, 'vareoncalc_settings_tip_notice_dismissed', '', true );
}

// Handle multisite vs single site.
if ( is_multisite() ) {
	$vareoncalc_site_ids = get_sites(
		array(
			'fields' => 'ids',
		)
	);

	foreach ( $vareoncalc_site_ids as $vareoncalc_site_id ) {
		switch_to_blog( $vareoncalc_site_id );
		vareoncalc_lite_cleanup_site_data();
		restore_current_blog();
	}

	// Clean up network-wide options if any.
	delete_site_option( 'vareoncalc_lite_options' );
	delete_site_option( 'vareoncalc_onboarding_status' );
	delete_site_option( 'vareoncalc_review_cta_state' );
} else {
	// Single site cleanup.
	vareoncalc_lite_cleanup_site_data();
}

// Clean up user meta (applies to all users across all sites).
vareoncalc_lite_cleanup_user_meta();
