<?php
/**
 * Uninstall cleanup. Runs from the Freemius `after_uninstall` hook, with a
 * plain WordPress uninstall-hook safety net (there is deliberately no
 * uninstall.php — see the main plugin file).
 *
 * @package Form_Flight_Recorder
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Uninstall routines.
 */
class Ffrd_Uninstall {

	/**
	 * Remove every trace: table, options, crons. The observed form plugins'
	 * own data is never touched (we never wrote any).
	 */
	public static function run() {
		global $wpdb;

		$table = $wpdb->prefix . 'ffrd_events';
		// Drop the plugin's own table on uninstall. Identifier is $wpdb->prefix + a
		// literal (no user input); a schema change is inherently uncacheable.
		$wpdb->query( "DROP TABLE IF EXISTS {$table}" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- own table; literal identifier.

		delete_option( 'ffrd_settings' );
		delete_option( 'ffrd_forms' );
		delete_option( 'ffrd_ack' );
		delete_option( 'ffrd_pro' );
		delete_option( 'ffrd_pro_state' );
		delete_option( 'ffrd_installed_at' );

		wp_clear_scheduled_hook( 'ffrd_tick_hourly' );
		wp_clear_scheduled_hook( 'ffrd_tick_daily' );
	}

	/**
	 * Core-hook safety net (only fires when the Freemius handler did not).
	 */
	public static function safety_net() {
		self::run();
	}
}
