<?php
/**
 * Uninstall script for Voice Logica AI for WooCommerce plugin
 *
 * @package VoiceLogica_AI_WooCommerce
 */

// If uninstall not called from WordPress, then exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	exit;
}

// Delete WooCommerce API key if it exists (before deleting the option).
$voicelogica_ai_api_key_id = absint( get_option( 'voicelogica_ai_api_key_id', 0 ) );
if ( $voicelogica_ai_api_key_id > 0 ) {
	global $wpdb;
	$voicelogica_ai_table_name = $wpdb->prefix . 'woocommerce_api_keys';
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall: check table exists and delete API key; no higher-level API.
	$voicelogica_ai_table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $voicelogica_ai_table_name ) ) === $voicelogica_ai_table_name;
	if ( $voicelogica_ai_table_exists ) {
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall: delete WooCommerce API key by ID.
		$wpdb->delete( $voicelogica_ai_table_name, array( 'key_id' => $voicelogica_ai_api_key_id ), array( '%d' ) );
	}
}

// Delete all plugin options.
delete_option( 'voicelogica_ai_webhook_url' );
delete_option( 'voicelogica_ai_connected' );
delete_option( 'voicelogica_ai_features' );
delete_option( 'voicelogica_ai_webhook_secret' );
delete_option( 'voicelogica_ai_api_key_id' );
delete_option( 'voicelogica_ai_api_consumer_key' );
delete_option( 'voicelogica_ai_api_consumer_secret' );
delete_option( 'voicelogica_ai_encryption_key' );
