<?php

/**
 * Content Craft AI Uninstaller
 *
 * This file runs when the plugin is uninstalled.
 * It removes all database tables and options created by the plugin.
 */

// If uninstall not called from WordPress, exit
if (!defined('WP_UNINSTALL_PLUGIN')) {
    exit;
}

// Delete database tables
global $wpdb;

// Tables to drop
$tables = array(
    $wpdb->prefix . 'content_craft_ai_generations',
    $wpdb->prefix . 'content_craft_ai_internal_links'
);

foreach ($tables as $table) {
    // Check if table exists before attempting to drop
    $table_exists = $wpdb->get_var("SHOW TABLES LIKE '$table'");

    if ($table_exists) {
        $wpdb->query("DROP TABLE IF EXISTS $table");
    }
}

// Delete plugin options
$options = array(
    'content_craft_ai_api_key',
    'content_craft_ai_token',
    'content_craft_ai_user_settings',
    'content_craft_ai_tutorial_status',
    'content_craft_ai_verification_token',
    'content_craft_ai_internal_links_updated'
);

foreach ($options as $option) {
    delete_option($option);
}

// Clear any scheduled cron events
wp_clear_scheduled_hook('content_craft_ai_process_scheduled_articles');
wp_clear_scheduled_hook('content_craft_ai_check_article_statuses');
wp_clear_scheduled_hook('content_craft_ai_check_activation');
