<?php


/**
 *
 * This file is read by WordPress to generate the plugin information in the plugin
 * admin area. This file also includes all of the dependencies used by the plugin,
 * registers the activation and deactivation functions, and defines a function
 * that starts the plugin.
 *
 * @link              https://insya.com/ai-magic
 * @since             2.0.0
 * @package           Ai_Magic
 *
 * @wordpress-plugin
 * Plugin Name:       Insya Magic — AI Content Generator for ChatGPT, Claude and Gemini
 * Plugin URI:        https://insya.com/ai-magic
 * Description:       Generate content with AI. Multi-provider support (OpenAI, Anthropic, Google, OpenRouter), templates, image generation and WooCommerce integration. One-click article generation.
 * Version:           2.2.2
 * Author:            AI Magic
 * Author URI:        https://insya.com/ai-magic/
 * License:           GPL-2.0+
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain:       ai-magic
 * Domain Path:       /languages
 */

// If this file is called directly, abort.
if (!defined('WPINC')) {
    die;
}

/**
 * Currently plugin version.
 * Rename this for your plugin and update it as you release new versions.
 */
define('AI_MAGIC_VERSION', '2.2.2');
define('AI_MAGIC_DB_VERSION', '1.0');

/**
 * The code that runs during plugin activation.
 * This action is documented in includes/class-ai-magic-activator.php
 */
function activate_ai_magic()
{
    require_once plugin_dir_path(__FILE__) . 'includes/class-ai-magic-activator.php';
    require_once plugin_dir_path(__FILE__) . 'includes/class-ai-magic-database.php';
    require_once plugin_dir_path(__FILE__) . 'includes/class-ai-magic-cost-calculator.php';
    Ai_Magic_Activator::activate();
    Ai_Magic_Database::create_tables();
}

/**
 * The code that runs during plugin deactivation.
 * This action is documented in includes/class-ai-magic-deactivator.php
 */
function deactivate_ai_magic()
{
    require_once plugin_dir_path(__FILE__) . 'includes/class-ai-magic-deactivator.php';
    Ai_Magic_Deactivator::deactivate();
}

register_activation_hook(__FILE__, 'activate_ai_magic');
register_deactivation_hook(__FILE__, 'deactivate_ai_magic');

/**
 * The core plugin class that is used to define internationalization,
 * admin-specific hooks, and public-facing site hooks.
 */
require plugin_dir_path(__FILE__) . 'includes/class-ai-magic.php';


// Layout-safe content update and theme wizard system.
require_once plugin_dir_path(__FILE__) . 'includes/class-ai-magic-editor-detector.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-ai-magic-layout-ai.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-ai-magic-content-service.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-ai-magic-theme-wizard-service.php';
require_once plugin_dir_path(__FILE__) . 'includes/processors/class-ai-magic-elementor-processor.php';
require_once plugin_dir_path(__FILE__) . 'includes/processors/class-ai-magic-gutenberg-processor.php';
require_once plugin_dir_path(__FILE__) . 'includes/processors/class-ai-magic-wpbakery-processor.php';
require_once plugin_dir_path(__FILE__) . 'includes/processors/class-ai-magic-classic-processor.php';
require_once plugin_dir_path(__FILE__) . 'includes/theme-scanners/class-ai-magic-customizer-scanner.php';
require_once plugin_dir_path(__FILE__) . 'includes/theme-scanners/class-ai-magic-widgets-scanner.php';
require_once plugin_dir_path(__FILE__) . 'includes/theme-scanners/class-ai-magic-menus-scanner.php';
require_once plugin_dir_path(__FILE__) . 'includes/theme-scanners/class-ai-magic-sliders-scanner.php';
require_once plugin_dir_path(__FILE__) . 'includes/theme-scanners/class-ai-magic-acf-scanner.php';

// Provider manager & interface (used to talk to AI backends).
if (!class_exists('AI_Magic_Provider_Manager')) {
    require_once plugin_dir_path(__FILE__) . 'admin/class-ai-magic-provider-manager.php';
}
if (!interface_exists('AI_Magic_Provider_Interface')) {
    $iface = plugin_dir_path(__FILE__) . 'includes/interface-ai-magic-provider.php';
    if (file_exists($iface)) {
        require_once $iface;
    }
}

/**
 * Helper: update a single post using AI without breaking its layout.
 *
 * @param int    $post_id
 * @param string $prompt
 * @return void
 */
function ai_magic_layout_update_post($post_id, $prompt)
{

    if (!class_exists('AI_Magic_Provider_Manager')) {
        return;
    }

    $post_id = absint($post_id);
    if (!$post_id) {
        return;
    }

    // Determine default provider and get its instance.
    $default_provider_id = AI_Magic_Provider_Manager::get_default_provider();
    $provider_instance = AI_Magic_Provider_Manager::get_provider_instance($default_provider_id);

    if (!is_object($provider_instance) || !method_exists($provider_instance, 'generateText')) {
        return;
    }

    $service = new AI_Magic_Content_Service($provider_instance);
    $service->process_post($post_id, $prompt);
}

// Admin UI for layout-safe content replacement & theme wizard.
if (is_admin()) {
    require_once plugin_dir_path(__FILE__) . 'admin/class-ai-magic-layout-admin.php';
    require_once plugin_dir_path(__FILE__) . 'admin/class-ai-magic-theme-wizard.php';
    new AI_Magic_Layout_Admin();
    new AI_Magic_Theme_Wizard_Admin();
}


/**
 * Begins execution of the plugin.
 *
 * Since everything within the plugin is registered via hooks,
 * then kicking off the plugin from this point in the file does
 * not affect the page life cycle.
 *
 * @since    1.0.0
 */
function run_ai_magic()
{

    $plugin = new Ai_Magic();
    $plugin->run();

}
run_ai_magic();
