<?php
/**
 * Fired when the plugin is uninstalled.
 *
 * Cleans up all plugin data: custom tables, options, transients, and post meta.
 * Only runs if the user has opted in via Settings → Auto Dealer → Advanced → "Delete all data on uninstall".
 *
 * @package ThemeREX\AutoDealer
 */

declare(strict_types=1);

// Prevent direct access.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

// Only delete data if the user opted in.
$delete_data = get_option( 'trx_ad_delete_data_on_uninstall', false );

if ( ! $delete_data ) {
	return;
}

global $wpdb;

// Remove custom tables.
$tables = array(
	$wpdb->prefix . 'trx_ad_vehicle_specs',
	$wpdb->prefix . 'trx_ad_price_history',
	$wpdb->prefix . 'trx_ad_test_drive_slots',
	$wpdb->prefix . 'trx_ad_custom_field_values',
);

foreach ( $tables as $table ) {
	$wpdb->query( "DROP TABLE IF EXISTS {$table}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}

// Remove all plugin options.
$wpdb->query(
	"DELETE FROM {$wpdb->options} WHERE option_name LIKE 'trx\_ad\_%'"
);

// Remove all plugin transients.
$wpdb->query(
	"DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_trx\_ad\_%' OR option_name LIKE '_transient_timeout_trx\_ad\_%'"
);

// Remove post meta.
$wpdb->query(
	"DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE '\_trx\_ad\_%'"
);

// Remove user meta.
$wpdb->query(
	"DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE 'trx\_ad\_%'"
);

// Remove term meta.
$wpdb->query(
	"DELETE FROM {$wpdb->termmeta} WHERE meta_key LIKE '\_trx\_ad\_%'"
);

// Remove custom post types and their posts.
$trx_post_types = array( 'trx_ad_listing', 'trx_ad_dealer', 'trx_ad_lead' );
foreach ( $trx_post_types as $trx_post_type ) {
	$trx_posts = get_posts(
		array(
			'post_type'     => $trx_post_type,
			'post_status'   => 'any',
			'numberposts'   => -1,
			'fields'        => 'ids',
			'no_found_rows' => true,
		)
	);
	foreach ( $trx_posts as $trx_post_id ) {
		wp_delete_post( $trx_post_id, true );
	}
}

// Remove custom taxonomies terms (core + user-defined).
$taxonomies = array(
	'trx_ad_make',
	'trx_ad_model',
	'trx_ad_body_style',
	'trx_ad_fuel_type',
	'trx_ad_transmission',
	'trx_ad_drivetrain',
	'trx_ad_color',
	'trx_ad_feature',
	'trx_ad_vehicle_type',
	'trx_ad_condition',
);

// Include user-defined taxonomy slugs.
$user_taxonomies = get_option( 'trx_ad_user_taxonomies', array() );
if ( is_array( $user_taxonomies ) ) {
	foreach ( $user_taxonomies as $user_tax ) {
		if ( isset( $user_tax['slug'] ) && is_string( $user_tax['slug'] ) ) {
			$taxonomies[] = $user_tax['slug'];
		}
	}
}

foreach ( $taxonomies as $trx_taxonomy ) {
	$terms = get_terms(
		array(
			'taxonomy'   => $trx_taxonomy,
			'hide_empty' => false,
			'fields'     => 'ids',
		)
	);
	if ( is_array( $terms ) ) {
		foreach ( $terms as $term_id ) {
			wp_delete_term( $term_id, $trx_taxonomy );
		}
	}
}

// Remove custom roles. remove_role() updates the core roles option itself;
// we only bust the object cache afterwards so a persistent cache can't keep
// serving a stale autoloaded roles blob (see the Pro uninstall note).
remove_role( 'trx_ad_dealer' );
remove_role( 'trx_ad_agent' );
wp_cache_delete( $wpdb->prefix . 'user_roles', 'options' );
wp_cache_delete( 'alloptions', 'options' );

// Flush rewrite rules.
flush_rewrite_rules();
