<?php

namespace AIPT\Core;

use AIPT\Api\Chatbot\Jobs\ChatbotJobWorker;
use AIPT\Core\Database\Migration;
use AIPT\Core\Database\HistoryCleaner;
use AIPT\Core\Database\JobLogCleaner;
use AIPT\Core\Database\RateLimiterCleaner;
use AIPT\Core\CreditSystem\CreditCache;

class Deactivator {
    public static function deactivate() {

        $admin_role = get_role('administrator');
        if ($admin_role) {
            $admin_role->remove_cap('manage_aipt_settings');
        }

        $shop_manager_role = get_role('shop_manager');
        if ($shop_manager_role) {
            $shop_manager_role->remove_cap('manage_aipt_settings');
        }

        $history_cleaner = new HistoryCleaner();
        $history_cleaner->unschedule();

        $log_cleaner = new JobLogCleaner();
        $log_cleaner->unschedule();

        $rate_limit_cleaner = new RateLimiterCleaner();
        $rate_limit_cleaner->unschedule();

        wp_clear_scheduled_hook( \AIPT\Core\Services\PricingFeedManager::CRON_HOOK );

        self::unschedule_credit_system_crons();

        self::unschedule_automation_jobs_crons();

        try {
            ChatbotJobWorker::unscheduleStaleRecovery();
        } catch ( \Throwable $throwable ) {

        }

        try {
            ChatbotJobWorker::unscheduleProcessJobActions();
        } catch ( \Throwable $throwable ) {

        }

        self::unschedule_store_index_jobs();

        update_option('aipt_needs_setup', true, false);
        update_option('aipt_setup_completed', false, false);

        self::clear_credit_cache();
    }

    /**
     * Kredi sistemi ile ilgili cron görevlerini durdurur
     *
     * @return void
     */
    private static function unschedule_credit_system_crons() {

        wp_clear_scheduled_hook('aipt_credit_sync');
    }

    /**
     * Automation jobs ile ilgili cron görevlerini durdurur
     *
     * @return void
     */
    private static function unschedule_automation_jobs_crons() {
        try {
            \AIPT\Api\AutomationJobs\JobScheduler::clearCronSchedules();
        } catch (\Exception $e) {

        }

        wp_clear_scheduled_hook('aipt_reap_stuck_jobs');
    }

    /**
     * AI Store Index ile ilgili Action Scheduler işlerini durdurur
     *
     * @return void
     */
    private static function unschedule_store_index_jobs() {

        if (!function_exists('as_unschedule_all_actions')) {
            return;
        }

        try {
            $group = 'aipt-store-index';

            as_unschedule_all_actions('aipt_build_store_index', [], $group);

            as_unschedule_all_actions('aipt_scheduled_index_rebuild', [], $group);

            update_option('aipt_store_index_status', 'idle', false);
            update_option('aipt_store_index_build_progress', 0, false);
            update_option('aipt_store_index_build_job_id', 0, false);

            delete_transient('aipt_index_build_data');

        } catch (\Exception $e) {

        }
    }

    /**
     * Kredi önbelleğini temizler
     *
     * @return void
     */
    private static function clear_credit_cache() {
        try {
            $cache = new CreditCache();
            $cache->clear();
        } catch (\Exception $e) {

        }
    }
}
