<?php

if (!function_exists('as_unschedule_all_actions')){
    require_once( plugin_dir_path( __FILE__ ) . '../libraries/action-scheduler-trunk/action-scheduler-trunk/action-scheduler.php');
}


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

/**
 * Fired during plugin deactivation.
 *
 * This class defines all code necessary to run during the plugin's deactivation.
 *
 * @since      1.0.0
 * @package    agile_tix
 * @subpackage agile_tix/includes
 * @author     AgileTix
 */
class agile_tix_Deactivator {

	/**
	 * Short Description. (use period)
	 *
	 * Long Description.
	 *
	 * @since    1.0.0
	 */
	public static function deactivate() {
        //clear_agile_data_cron();
        update_option('agile_field_webhook_is_active', false);
        //register_deactivation_hook(__FILE__, 'clear_agile_data_cron');
        delete_transient('agile_api_response');
        //register_deactivation_hook(__FILE__,'agile_tix_Rest_Route::agile_call_wordpress_webhook(false)');
        agile_tix_Rest_Route::agile_call_wordpress_webhook(false);

        agile_tix_Deactivator::agile_cleanup_orphaned_chunks();

        agile_tix_Deactivator::clear_scheduled_actions();
	}


    function clear_agile_data_cron()
    {
        // Unschedule the cron job if it's scheduled
        $timestamp = wp_next_scheduled('fetch_agile_data_cron_event');
        if ($timestamp) {
            wp_unschedule_event($timestamp, 'fetch_agile_data_cron_event');
        }
    }

    static function agile_cleanup_orphaned_chunks() {
        global $wpdb;
    
        // Get all chunk options
        $chunk_options = $wpdb->get_results(
            "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'agile_chunk_data_%'",
            ARRAY_A
        );
    
        //$cutoff_time = time() - (24 * 60 * 60); // 24 hours ago
    
        foreach ($chunk_options as $option) {
            $option_name = $option['option_name'];
        
            if (preg_match('/agile_chunk_data_(\d+)$/', $option_name, $matches)) {
                $timestamp = (int) $matches[1];
            
                // Delete if older than 24 hours
                //if ($timestamp < $cutoff_time) {
                    delete_option($option_name);
                //}
            }
        }
    }

    static function clear_scheduled_actions() {
        // Check if Action Scheduler is available
        if (!function_exists('as_unschedule_all_actions')) {
            return;
        }
    
        as_unschedule_all_actions('', [], 'agile_events_processing');
    
        if (function_exists('as_get_scheduled_actions')) {
            $all_statuses = [
                ActionScheduler_Store::STATUS_COMPLETE,
                ActionScheduler_Store::STATUS_FAILED,
                ActionScheduler_Store::STATUS_CANCELED
            ];
        
            foreach ($all_statuses as $status) {
                $actions = as_get_scheduled_actions([
                    'group' => 'agile_events_processing',
                    'status' => $status,
                    'per_page' => -1 // Get all
                ]);
            }
        }
    }


}
