<?php
/**
 * This file is part of the Wisewand WordPress plugin.
 *
 * @package Wisewand
 * @since 1.0.0
 * @license GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */
namespace Wisewand\Core;

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Deactivator {

	public static function deactivate() {
		// Clear scheduled cron events
		$timestamp = wp_next_scheduled( 'wisewand_cleanup_logs' );
		if ( $timestamp ) {
			wp_unschedule_event( $timestamp, 'wisewand_cleanup_logs' );
		}

		// Clear editorial calendar CRON
		$calendar_timestamp = wp_next_scheduled( 'wisewand_editorial_calendar_cron' );
		if ( $calendar_timestamp ) {
			wp_unschedule_event( $calendar_timestamp, 'wisewand_editorial_calendar_cron' );
		}

		// Clear all pull retry cron jobs
		self::clear_pull_retry_crons();

		// Flush rewrite rules
		flush_rewrite_rules();
	}

	private static function clear_pull_retry_crons() {
		global $wpdb;

		// Get all scheduled pull retries
		$table_name = $wpdb->prefix . 'wisewand_pull_queue';

		// v2.1.3: FIX - Check if table exists before querying
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ) ) === $table_name;

		if ( ! $table_exists ) {
			// Table doesn't exist (fresh install or different database), skip cleanup
			return;
		}

        // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name from $wpdb->prefix is safe to interpolate; custom plugin table query to retrieve pending articles for CRON cleanup during deactivation
		// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter -- Table name from $wpdb->prefix is safe
		$pending_articles = $wpdb->get_col(
			"SELECT article_id FROM $table_name WHERE status = 'pending'"
		);
        // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching

		// Clear cron events for each pending article
		foreach ( $pending_articles as $article_id ) {
			$timestamp = wp_next_scheduled( 'wisewand_pull_retry', array( $article_id ) );
			if ( $timestamp ) {
				wp_unschedule_event( $timestamp, 'wisewand_pull_retry', array( $article_id ) );
			}
		}
	}
}
