<?php
/**
 * Uninstall cleanup.
 *
 * There is deliberately no uninstall.php. WordPress runs EITHER uninstall.php
 * OR the registered uninstall hook, never both — and when uninstall.php exists,
 * core actively DELETES the registered hook from the `uninstall_plugins` option
 * before running it (wp-admin/includes/plugin.php, uninstall_plugin()).
 * Shipping one would silently disable the Freemius SDK's uninstall handling,
 * which is why the Freemius deployment processor rejects a plugin that has one.
 *
 * Cleanup therefore hangs off Freemius's `after_uninstall` action (which fires
 * for anonymous users too — the registration check in class-freemius.php guards
 * only the server call, not the action). Because that action does NOT fire when
 * the SDK instance cannot be resolved, safety_net() below registers on the core
 * `uninstall_{basename}` hook as well, at a late priority, and no-ops when the
 * SDK path already cleaned up.
 *
 * Generated invoice documents in uploads are legal records the merchant must
 * retain (10 years in France) — they are intentionally NOT deleted. Documented
 * in the readme FAQ.
 *
 * @package FEPW
 */

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

/**
 * Class FEPW_Uninstall
 */
class FEPW_Uninstall {

	/**
	 * Has the cleanup already run this request? Both entry points may fire;
	 * only one should do the work.
	 *
	 * @var bool
	 */
	private static $done = false;

	/**
	 * Every option this plugin has ever written.
	 *
	 * @return string[]
	 */
	private static function options() {
		return array(
			'fepw_settings',
			'fepw_db_version',
			'fepw_checklist',
			'fepw_docs_token',
			'fepw_last_facturx_error',
			// Written only by the paid build's Pro layer; deleted here so
			// uninstalling the free copy leaves no residue. Never read here.
			'fepro_connector',
			'fepro_transmission_log',
		);
	}

	/**
	 * The Freemius-sanctioned entry point: fires on `after_uninstall`.
	 */
	public static function run() {
		self::cleanup();
	}

	/**
	 * The belt-and-braces entry point on core's own uninstall hook.
	 *
	 * WordPress include_once's the plugin's main file before firing the
	 * uninstall hook, so this class is loadable with no dependency on the
	 * Freemius SDK being healthy. Late priority: the SDK's handler goes first.
	 */
	public static function safety_net() {
		if ( self::$done ) {
			return; // The SDK path already cleaned up.
		}
		self::cleanup();
	}

	/**
	 * The actual cleanup: own tables, options, transients. Never the documents.
	 */
	private static function cleanup() {
		self::$done = true;
		global $wpdb;

		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fepw_sequences" );  // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery -- own table, uninstall.
		$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fepw_documents" );  // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery -- own table, uninstall.

		foreach ( self::options() as $fepw_option ) {
			delete_option( $fepw_option );
		}

		$wpdb->query( "DELETE FROM {$wpdb->options} WHERE option_name LIKE '\_transient\_fepw\_%' OR option_name LIKE '\_transient\_timeout\_fepw\_%' OR option_name LIKE '\_site\_transient\_fepw\_%' OR option_name LIKE '\_site\_transient\_timeout\_fepw\_%'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- transient sweep at uninstall.
	}
}
