<?php
/**
 * Fired during plugin deactivation.
 *
 * @package BoostBot
 * @since   1.0.0
 */

// Prevent direct access
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Class BoostBot_Deactivator
 *
 * This class handles all logic executed during plugin deactivation.
 * Note: Data is NOT deleted here - that happens in uninstall.php
 *
 * @since 1.0.0
 */
class BoostBot_Deactivator {

    /**
     * Deactivate the plugin.
     *
     * @return void
     */
    public static function deactivate() {
        // Clear all scheduled cron events
        self::clear_scheduled_events();

        // Clear transients
        self::clear_transients();

        // Flush rewrite rules
        flush_rewrite_rules();

        // Log deactivation
        if ( defined( 'WP_DEBUG' ) && WP_DEBUG && class_exists( 'BoostBot_Logger' ) ) {
            BoostBot_Logger::info( 'BoostBot deactivated' );
        }
    }

    /**
     * Clear all scheduled cron events.
     *
     * @return void
     */
    private static function clear_scheduled_events() {
        $cron_hooks = array(
            'boostbot_daily_analytics',
            'boostbot_cleanup_old_conversations',
            'boostbot_cache_cleanup',
            'boostbot_check_content_updates',
            'boostbot_generate_reports',
            'boostbot_optimize_database',
            'boostbot_usage_report',
            'boostbot_reset_monthly_counters',
            'boostbot_incremental_index',
            'boostbot_webhook_retry',
            // Indexing (new + legacy)
            'boostbot_process_index_batch',
            'boostbot_index_recover_stuck',
            'boostbot_process_index_queue',
            // Low-credits monitor
            'boostbot_low_credits_check',
            // Registration monitor
            'boostbot_registration_health_check',
            'boostbot_registration_on_activate',
        );

        foreach ( $cron_hooks as $hook ) {
            $timestamp = wp_next_scheduled( $hook );
            if ( $timestamp ) {
                wp_unschedule_event( $timestamp, $hook );
            }

            // Also clear all events with this hook
            wp_clear_scheduled_hook( $hook );
        }
    }

    /**
     * Clear plugin transients.
     *
     * @return void
     */
    private static function clear_transients() {
        global $wpdb;

        // Delete transients with boostbot prefix
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
        $wpdb->query(
            // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
            "DELETE FROM {$wpdb->options} 
             WHERE option_name LIKE '_transient_boostbot_%'
             OR option_name LIKE '_transient_timeout_boostbot_%'"
        );

        // Delete specific known transients
        $transients = array(
            'boostbot_activation_redirect',
            'boostbot_api_status',
            'boostbot_usage_data',
            'boostbot_index_status',
        );

        foreach ( $transients as $transient ) {
            delete_transient( $transient );
        }
    }

    /**
     * Remove user capabilities.
     *
     * Note: We DON'T remove capabilities on deactivation,
     * only on full uninstall. This preserves settings if
     * the user reactivates the plugin.
     *
     * @return void
     */
    public static function remove_capabilities() {
        $admin_role = get_role( 'administrator' );

        if ( $admin_role ) {
            $capabilities = array(
                'manage_boostbot',
                'view_boostbot_leads',
                'edit_boostbot_leads',
                'delete_boostbot_leads',
                'export_boostbot_leads',
                'view_boostbot_conversations',
                'manage_boostbot_settings',
                'manage_boostbot_knowledge',
            );

            foreach ( $capabilities as $cap ) {
                $admin_role->remove_cap( $cap );
            }
        }
    }
}










