<?php

if (!defined('ABSPATH')) {
  exit;
}

/**
 * Register cron event for checking article statuses
 */
function content_craft_ai_register_article_statuses_cron()
{
  // Ensure the interval is registered (it's registered in scheduled-articles.php, but good to have a check or ensure load order)
  if (!wp_next_scheduled('content_craft_ai_check_article_statuses')) {
    wp_schedule_event(time(), 'minute', 'content_craft_ai_check_article_statuses');
  }
}

/**
 * Check statuses of pending or processing articles
 * This function delegates the actual processing to the StatusCheckController
 */
function content_craft_ai_check_article_statuses()
{
  // Ensure the controller is loaded
  // Adjust path as necessary if your autoloader or include structure is different
  if (!class_exists('ContentCraftAI_StatusCheckController')) {
    require_once CONTENT_CRAFT_AI_PLUGIN_PATH . 'app/controllers/article-generator/StatusCheckController.php';
  }

  // Get the controller instance
  $controller = ContentCraftAI_StatusCheckController::get_instance();

  // Process pending statuses and get statistics
  $stats = $controller->check_pending_statuses();

  // Optional: Log or use the stats as needed
  // error_log('Content Craft AI: Checked article statuses. Stats: ' . json_encode($stats));
}

// Register the cron event (can be called on 'init' or ensure activation hook covers it)
add_action('init', 'content_craft_ai_register_article_statuses_cron');

// Hook the callback function to the scheduled event
add_action('content_craft_ai_check_article_statuses', 'content_craft_ai_check_article_statuses');
