<?php
/**
 * Uninstall handler for Rewind Form Bridge for Brevo.
 *
 * Drops tables and deletes options ONLY when the site owner explicitly
 * opted in via the "Delete data on uninstall" setting. Default is to keep
 * everything — delivery logs and consent records are compliance data.
 *
 * @package RewindBrevoFormBridge
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

$rbfb_settings = get_option( 'rbfb_settings', array() );

if ( empty( $rbfb_settings['delete_data_on_uninstall'] ) ) {
	return;
}

global $wpdb;

foreach ( array( 'rbfb_feeds', 'rbfb_log', 'rbfb_consent' ) as $rbfb_table ) {
	// %i is the identifier placeholder (WP 6.2+; this plugin requires 6.6),
	// so even the table name in DROP TABLE goes through prepare().
	$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . $rbfb_table ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
}

delete_option( 'rbfb_settings' );
delete_option( 'rbfb_db_version' );
delete_option( 'rbfb_failure_streak' );
delete_option( 'rbfb_last_verified' );
delete_option( 'rbfb_cache_stamp' );
delete_option( 'rbfb_email_last_error' );
delete_option( 'rbfb_email_last_sent' );
