<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Plugin Deactivation Handler
 *
 * Handles cleanup of scheduled events and temporary data
 *
 * @package    Kitsune_AI_Platform
 * @subpackage Kitsune_AI_Platform/includes
 */

class Kitsune_AI_Platform_Deactivator {

    /**
     * Deactivate the plugin
     *
     * Clears scheduled cron jobs and performs cleanup
     * Note: Does not delete database tables or permanent data
     */
    public static function deactivate() {
        self::clear_scheduled_events();
        self::clear_cache();
        flush_rewrite_rules();
    }

    /**
     * Clear all scheduled WordPress cron events
     */
    private static function clear_scheduled_events() {
        $scheduled_hooks = array(
            'kitsune_ai_platform_daily_stats',
            'kitsune_ai_platform_weekly_summary',
            'kitsune_ai_platform_cleanup_logs',
        );

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

        // Clear all instances of the hooks
        wp_clear_scheduled_hook('kitsune_ai_platform_daily_stats');
        wp_clear_scheduled_hook('kitsune_ai_platform_weekly_summary');
        wp_clear_scheduled_hook('kitsune_ai_platform_cleanup_logs');

        // Clear all individual scheduled-task cron events
        wp_clear_scheduled_hook('kitsune_ai_platform_run_scheduled_task');
    }

    /**
     * Clear temporary cache files
     */
    private static function clear_cache() {
        $upload_dir = wp_upload_dir();
        $cache_dir = $upload_dir['basedir'] . '/ai-platform/cache';

        if (file_exists($cache_dir)) {
            self::delete_directory_contents($cache_dir);
        }

        // Clear WordPress transients
        global $wpdb;
        $wpdb->query(
            $wpdb->prepare(
                "DELETE FROM %i WHERE option_name LIKE %s OR option_name LIKE %s",
                $wpdb->options,
                '_transient_ai_platform_%',
                '_transient_timeout_ai_platform_%'
            )
        );
    }

    /**
     * Recursively delete directory contents
     *
     * @param string $dir Directory path
     */
    private static function delete_directory_contents($dir) {
        global $wp_filesystem;

        if (empty($wp_filesystem)) {
            require_once ABSPATH . 'wp-admin/includes/file.php';
            WP_Filesystem();
        }

        if (!$wp_filesystem || !$wp_filesystem->is_dir($dir)) {
            return;
        }

        $files = $wp_filesystem->dirlist($dir);
        if ($files) {
            foreach ($files as $file) {
                $path = trailingslashit($dir) . $file['name'];
                if ($file['type'] === 'd') {
                    self::delete_directory_contents($path);
                    $wp_filesystem->rmdir($path);
                } else {
                    $wp_filesystem->delete($path);
                }
            }
        }
    }
}
