<?php
// phpcs:disable WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL,WordPress.DB.PreparedSQLPlaceholders -- Deactivation performs maintenance on a fixed set of plugin-owned tables.

/**
 * Fired during plugin deactivation
 *
 * @link       https://example.com
 * @since      1.0.0
 *
 * @package    Instant_Popup_Builder
 * @subpackage Instant_Popup_Builder/includes
 */

/**
 * Fired during plugin deactivation.
 *
 * This class defines all code necessary to run during the plugin's deactivation.
 *
 * NOTE: WordPress best practice is to NOT delete data on deactivation, only on uninstall.
 * However, this plugin provides a "Delete All Popups" setting that some users may want
 * to trigger on deactivation. The proper cleanup happens in uninstall.php.
 *
 * @since      1.0.0
 * @package    Instant_Popup_Builder
 * @subpackage Instant_Popup_Builder/includes
 * @author     7th Sky Software's <example@gmail.com>
 */
class Instant_Popup_Builder_Deactivator {

	/**
	 * Plugin deactivation handler.
	 *
	 * Optionally deletes popup data based on user settings.
	 * Respects the "Delete All Popups" setting configured in plugin settings.
	 *
	 * @since    1.0.0
	 */
	public static function deactivate() {

		global $wpdb;

		// Get user setting for data deletion on deactivation
		$delete_popups      = get_option( 'instant_popup_builder_db_active', 'no' );
		$preserve_analytics = get_option( 'instant_popup_builder_preserve_analytics', 'yes' );

		// Only proceed if user has enabled "Delete All Popups"
		if ( 'yes' !== $delete_popups ) {
			return;
		}

		$prefix = $wpdb->prefix;

		// Delete popup data from main table
		$popup_table         = $prefix . 'instant_popup_builder';
		$escaped_popup_table = esc_sql( $popup_table );

		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
		$popup_ids = $wpdb->get_col( "SELECT id FROM `{$escaped_popup_table}`" );

		if ( ! is_array( $popup_ids ) || empty( $popup_ids ) ) {
			// No popups to delete
			return;
		}

		// Prepare placeholders for safe deletion
		$placeholders = implode( ',', array_fill( 0, count( $popup_ids ), '%d' ) );

		// Delete popup records
		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
		$wpdb->query( $wpdb->prepare( "DELETE FROM `{$escaped_popup_table}` WHERE `id` IN ($placeholders)", ...$popup_ids ) );

		// Delete subscription settings associated with deleted popups
		$subscription_settings_table         = $prefix . 'instant_popup_subscription_settings';
		$escaped_subscription_settings_table = esc_sql( $subscription_settings_table );

		// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
		$wpdb->query( $wpdb->prepare( "DELETE FROM `{$escaped_subscription_settings_table}` WHERE `popup_id` IN ($placeholders)", ...$popup_ids ) );

		// Handle analytics deletion based on preserve setting
		if ( 'no' === $preserve_analytics ) {
			$analytics_table         = $prefix . 'instant_popup_analytics';
			$escaped_analytics_table = esc_sql( $analytics_table );

			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching
			$wpdb->query( $wpdb->prepare( "DELETE FROM `{$escaped_analytics_table}` WHERE `popup_id` IN ($placeholders)", ...$popup_ids ) );
		}

		// Allow extensions to clean up their data for deleted popups
		do_action( 'instant_popup_builder_deactivate_cleanup', $popup_ids );
	}

}
