<?php
/**
 * Plugin Deactivator
 *
 * @package PackshotPhotoAI
 */

namespace PackshotPhotoAI\Core;

defined('ABSPATH') || exit;

/**
 * Runs on plugin deactivation
 */
class Deactivator {

    /**
     * Deactivate plugin
     */
    public static function deactivate(): void {
        delete_transient('packshotphoto_api_test_result');

        wp_clear_scheduled_hook('packshotphoto_bulk_worker');
        wp_clear_scheduled_hook('packshotphoto_cleanup_trash');
        wp_clear_scheduled_hook('packshotphoto_keep_alive_event');

        self::cleanup_trash_folder();

        flush_rewrite_rules();
    }

    private static function cleanup_trash_folder(): void {
        $upload_dir = wp_upload_dir();
        $trash_dir = $upload_dir['basedir'] . '/packshotphoto-trash';

        if (is_dir($trash_dir)) {
            global $wp_filesystem;
            if (empty($wp_filesystem)) {
                require_once ABSPATH . 'wp-admin/includes/file.php';
                WP_Filesystem();
            }
            if (!empty($wp_filesystem)) {
                $wp_filesystem->delete($trash_dir, true);
            }
        }
    }
}
