<?php
/**
 * affilun uninstall handler.
 *
 * This file is executed by WordPress when the plugin is deleted from the
 * Plugins screen. It MUST only be reached via the WordPress uninstall
 * lifecycle; direct access is blocked by the WP_UNINSTALL_PLUGIN guard.
 *
 * Uninstall policy:
 *
 * - Secret-bearing and operational options are removed by default. This wipes
 *   provider credentials, OpenAI keys, affilun Cloud API keys, click option
 *   aggregates, cron cursors, worker identifiers, and schema markers stored in
 *   WordPress options.
 * - Scheduled cron hooks owned by affilun are unscheduled.
 * - Custom product/log/audit/sync tables are preserved for now. Destructive
 *   table drops require an explicit purge-on-uninstall product decision and a
 *   dedicated migration/uninstall slice.
 * - Multisite installs are cleaned per-site when the standard multisite APIs
 *   are available.
 *
 * @package Affilun
 */

declare(strict_types=1);

// Exit if not called from the WordPress uninstall lifecycle.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

// Extra belt-and-braces guard: ABSPATH must also be defined in a real
// WordPress context. Without it we refuse to proceed.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Delete options that either carry secret material or short-lived operational
 * state.
 */
function affilun_uninstall_delete_options(): void {
	if ( ! function_exists( 'delete_option' ) ) {
		return;
	}

	$optionKeys = array(
		'affilun_settings',
		'affilun_settings_secrets',
		'affilun_amazon_discount_priority_brands',
		'affilun_affiliate_routing_templates',
		'affilun_click_events',
		'affilun_click_counts',
		'affilun_link_health_cron_state',
		'affilun_existing_post_scan_jobs',
		'affilun_sync_worker_id',
		'affilun_db_schema_version',
	);

	foreach ( $optionKeys as $optionKey ) {
		delete_option( $optionKey );
	}
}

/**
 * Unschedule affilun-owned cron hooks.
 */
function affilun_uninstall_unschedule_hooks(): void {
	if ( ! function_exists( 'wp_clear_scheduled_hook' ) ) {
		return;
	}

	$hooks = array(
		'affilun_cron_dispatch_jobs',
		'affilun_cron_periodic_sync',
		'affilun_cron_audit_retention_purge',
		'affilun_cron_link_health_batch',
	);

	foreach ( $hooks as $hook ) {
		wp_clear_scheduled_hook( $hook );
	}
}

/**
 * Clean the currently selected site/blog.
 */
function affilun_uninstall_cleanup_site(): void {
	affilun_uninstall_unschedule_hooks();
	affilun_uninstall_delete_options();
}

/**
 * Clean every site on multisite installs when possible, otherwise clean only
 * the current site.
 */
function affilun_uninstall_cleanup(): void {
	if (
		function_exists( 'is_multisite' )
		&& is_multisite()
		&& function_exists( 'get_sites' )
		&& function_exists( 'switch_to_blog' )
		&& function_exists( 'restore_current_blog' )
	) {
		$siteIds = get_sites( array( 'fields' => 'ids' ) );
		foreach ( $siteIds as $siteId ) {
			switch_to_blog( (int) $siteId );
			affilun_uninstall_cleanup_site();
			restore_current_blog();
		}
		return;
	}

	affilun_uninstall_cleanup_site();
}

affilun_uninstall_cleanup();
