<?php
/**
 * Uninstall Script
 *
 * Fired when the plugin is uninstalled.
 * Cleans up all plugin data from the database.
 *
 * @package SmartyPress_AI_Engine
 * @since 1.0.0
 */

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

/**
 * Delete plugin options
 */
function smartypress_ai_delete_options() {
	// Delete main settings
	delete_option( 'smartypress_ai_settings' );
	delete_option( 'smartypress_ai_activated_at' );
	delete_option( 'smartypress_ai_image_alt_settings' );
	delete_option( 'smartypress_ai_image_alt_job' );
	delete_option( 'smartypress_ai_image_alt_index' );
	delete_option( 'smartypress_ai_image_alt_index_db_version' );
	delete_option( 'smartypress_ai_yandex_reauth_required' );
	
	// Delete any transients
	delete_transient( 'smartypress_ai_cache' );
	
	// On multisite, delete options for all sites
	if ( is_multisite() ) {
		global $wpdb;
		
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for multisite cleanup during uninstall
		$blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
		$original_blog_id = get_current_blog_id();
		
		foreach ( $blog_ids as $blog_id ) {
			switch_to_blog( $blog_id );
			
			delete_option( 'smartypress_ai_settings' );
			delete_option( 'smartypress_ai_activated_at' );
			delete_option( 'smartypress_ai_image_alt_settings' );
			delete_option( 'smartypress_ai_image_alt_job' );
			delete_option( 'smartypress_ai_image_alt_index' );
			delete_option( 'smartypress_ai_image_alt_index_db_version' );
			delete_option( 'smartypress_ai_yandex_reauth_required' );
			delete_transient( 'smartypress_ai_cache' );
		}
		
		switch_to_blog( $original_blog_id );
	}
}

/**
 * Delete post meta created by the plugin
 */
function smartypress_ai_delete_post_meta() {
	global $wpdb;
	
	// Delete post meta (if plugin created any post metadata)
	$pattern = 'smartypress_ai_';
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for bulk cleanup during uninstall
	$wpdb->query(
		$wpdb->prepare(
			"DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
			$wpdb->esc_like( $pattern ) . '%'
		)
	);
}

/**
 * Delete user meta created by the plugin
 */
function smartypress_ai_delete_user_meta() {
	global $wpdb;
	
	// Delete user meta (if plugin created any user metadata)
	$pattern = 'smartypress_ai_';
	// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for bulk cleanup during uninstall
	$wpdb->query(
		$wpdb->prepare(
			"DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s",
			$wpdb->esc_like( $pattern ) . '%'
		)
	);
}

/**
 * Delete custom database tables (if any)
 */
function smartypress_ai_delete_custom_tables() {
	global $wpdb;

	// Drop token usage tracking table.
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}smartypress_ai_token_usage" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}smartypress_ai_image_alt_index" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange

	delete_option( 'smartypress_ai_token_usage_db_version' );
	delete_option( 'smartypress_ai_image_alt_index_db_version' );
}

/**
 * Clear scheduled cron events
 */
function smartypress_ai_clear_scheduled_events() {
	$hooks = array(
		'smartypress_ai_daily_cleanup',
		'smartypress_ai_token_usage_cleanup',
		'smartypress_ai_image_alt_auto_generate',
	);
	foreach ( $hooks as $hook ) {
		wp_clear_scheduled_hook( $hook );
	}
}

/**
 * Run uninstall procedures
 */
function smartypress_ai_uninstall() {
	// Check if user has permission to delete plugins
	if ( ! current_user_can( 'delete_plugins' ) ) {
		return;
	}
	
	// Delete options
	smartypress_ai_delete_options();
	
	// Delete post meta
	smartypress_ai_delete_post_meta();
	
	// Delete user meta
	smartypress_ai_delete_user_meta();
	
	// Delete custom tables
	smartypress_ai_delete_custom_tables();
	
	// Clear scheduled events
	smartypress_ai_clear_scheduled_events();
	
	// Clear any cached data
	wp_cache_flush();
}

// Run the uninstall
smartypress_ai_uninstall();


