<?php
/**
 * Uninstall script for BW Lead Attribution Intelligence.
 *
 * Runs when the plugin is deleted via the WP Admin plugins screen.
 * Cleans up options, the handoff table, and the prune cron.
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

// Delete plugin options.
delete_option( 'bw_lead_ai_settings' );
delete_option( 'bw_lead_ai_utm_tracking' );
delete_option( 'bw_lead_ai_version' );
delete_option( 'bw_lead_ai_handoff_schema' );

// Delete site meta on multisite.
if ( is_multisite() ) {
	delete_site_option( 'bw_lead_ai_settings' );
}

// Stop the handoff prune cron.
$bw_lead_ai_prune = wp_next_scheduled( 'bw_lead_ai_handoff_prune' );
if ( $bw_lead_ai_prune ) {
	wp_unschedule_event( $bw_lead_ai_prune, 'bw_lead_ai_handoff_prune' );
}

// Drop the handoff table. Created lazily on first enable, so it may not exist.
global $wpdb;
$bw_lead_ai_table = $wpdb->prefix . 'bw_lead_ai_handoffs';
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name built from $wpdb->prefix.
$wpdb->query( "DROP TABLE IF EXISTS {$bw_lead_ai_table}" );
