<?php

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

/**
 * Plugin activation handler
 */
function content_craft_ai_activate_plugin()
{
    // Make sure cron intervals are registered first
    content_craft_ai_add_cron_interval(array());

    // Schedule our cron event
    if (!wp_next_scheduled('content_craft_ai_check_scheduled_articles')) {
        wp_schedule_event(time(), 'minute', 'content_craft_ai_check_scheduled_articles');
    }

    // Schedule cron for checking article statuses
    if (!wp_next_scheduled('content_craft_ai_check_article_statuses')) {
        wp_schedule_event(time(), 'minute', 'content_craft_ai_check_article_statuses');
    }

    // Create necessary database tables if they don't exist
    // We'll implement this later
}

/**
 * Plugin deactivation handler
 */
function content_craft_ai_deactivate_plugin()
{
    // Remove the scheduled cron job
    $timestamp = wp_next_scheduled('content_craft_ai_check_scheduled_articles');
    if ($timestamp) {
        wp_unschedule_event($timestamp, 'content_craft_ai_check_scheduled_articles');
    }

    // Remove the scheduled cron job for article statuses
    $status_timestamp = wp_next_scheduled('content_craft_ai_check_article_statuses');
    if ($status_timestamp) {
        wp_unschedule_event($status_timestamp, 'content_craft_ai_check_article_statuses');
    }
}

// Register activation/deactivation hooks
register_activation_hook(CONTENT_CRAFT_AI_PLUGIN_PATH . 'content-craft-ai.php', 'content_craft_ai_activate_plugin');
register_deactivation_hook(CONTENT_CRAFT_AI_PLUGIN_PATH . 'content-craft-ai.php', 'content_craft_ai_deactivate_plugin');
