<?php

namespace ConflictRadar\Core;

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

/**
 * Cleanup handler intended to be registered via the Freemius `after_uninstall` action
 * (NOT uninstall.php) — wiring will be added in Phase 5 once Freemius is integrated.
 * Drops all four custom tables and deletes all conflict_radar_* options.
 */
class Uninstaller {

	public static function cleanup(): void {
		global $wpdb;

		$tables = array(
			$wpdb->prefix . 'conflict_radar_correlations',
			$wpdb->prefix . 'conflict_radar_alerts',
			$wpdb->prefix . 'conflict_radar_logs',
			$wpdb->prefix . 'conflict_radar_events',
		);

		foreach ( $tables as $table ) {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Intentional table teardown on uninstall. $table is $wpdb->prefix + a hardcoded literal (no user input); DROP TABLE identifiers cannot be prepared.
			$wpdb->query( "DROP TABLE IF EXISTS {$table}" );
		}

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- One-shot uninstall cleanup of all conflict_radar_* options; core {$wpdb->options} table, hardcoded LIKE pattern.
		$wpdb->query(
			"DELETE FROM {$wpdb->options} WHERE option_name LIKE 'conflict_radar_%'"
		);
	}
}
