<?php

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

/**
 * Register cron event for checking scheduled articles
 */
function content_craft_ai_register_scheduled_articles_cron()
{
    if (!wp_next_scheduled('content_craft_ai_check_scheduled_articles')) {
        wp_schedule_event(time(), 'minute', 'content_craft_ai_check_scheduled_articles');
    }
}

/**
 * Check for scheduled articles that need to be generated
 * This function delegates the actual processing to the controller
 */
function content_craft_ai_check_scheduled_articles()
{
    // Get the controller instance
    $controller = ContentCraftAI_ScheduledArticleController::get_instance();

    // Process scheduled articles and get statistics
    $stats = $controller->process_scheduled_articles();

    // Optional: Log or use the stats as needed
}

// Register the cron event on plugin activation
add_action('init', 'content_craft_ai_register_scheduled_articles_cron');

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

// Add custom cron interval
function content_craft_ai_add_cron_interval($schedules)
{
    $schedules['minute'] = array(
        'interval' => 60,
        'display'  => esc_html__('Every Minute', 'content-craft-ai'),
    );
    return $schedules;
}
add_filter('cron_schedules', 'content_craft_ai_add_cron_interval');
