<?php
/**
 * Plugin Name: ContentGen AI Product Content & Image Generator
 * Plugin URI: https://wordpress.org/plugins/contengen-ai-product-content-image-generator
 * Description: Generate compelling product descriptions, titles, and images using AI for WooCommerce products. Supports multiple AI providers including OpenAI, Claude, and Gemini.
 * Version: 1.0.0
 * Author: SH Sajal Chowdhury
 * Author URI: https://easywptools.com
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: contengen-ai-product-content-image-generator
 * Domain Path: /languages
 * Requires at least: 5.0
 * Tested up to: 6.9
 * Requires PHP: 7.4
 * WC requires at least: 5.0
 * WC tested up to: 9.3
 *
 * @package AIProductContentImageGenerator
 */

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

$apcig_active_plugins = apply_filters('apcig_active_plugins', get_option('active_plugins', []));
if (!in_array('woocommerce/woocommerce.php', $apcig_active_plugins, true)) {
    add_action('admin_notices', function() {
        echo '<div class="notice notice-error"><p>ContentGen AI Product Content & Image Generator requires WooCommerce to be installed and activated.</p></div>';
    });
    return;
}

define('APCIG_VERSION', '1.0.0');
define('APCIG_PLUGIN_FILE', __FILE__);
define('APCIG_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('APCIG_PLUGIN_URL', plugin_dir_url(__FILE__));
define('APCIG_PLUGIN_BASENAME', plugin_basename(__FILE__));
define('APCIG_ASSETS_URL', APCIG_PLUGIN_URL . 'assets/');
define('APCIG_TEMPLATES_DIR', APCIG_PLUGIN_DIR . 'templates/');

require_once APCIG_PLUGIN_DIR . 'vendor/autoload.php';

add_action('before_woocommerce_init', function() {
    if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
    }
});

add_action('plugins_loaded', function() {
    if (version_compare(PHP_VERSION, '7.4', '<')) {
        add_action('admin_notices', function() {
            echo '<div class="notice notice-error"><p>';
            echo esc_html('ContentGen AI Product Content & Image Generator requires PHP 7.4 or higher.');
            echo '</p></div>';
        });
        return;
    }
    
    AIProductContentImageGenerator\Plugin::getInstance();
});

register_activation_hook(__FILE__, function() {
    if (version_compare(PHP_VERSION, '7.4', '<')) {
        deactivate_plugins(plugin_basename(__FILE__));
        wp_die(
            esc_html('ContentGen AI Product Content & Image Generator requires PHP 7.4 or higher.'),
            esc_html('Plugin Activation Error'),
            ['back_link' => true]
        );
    }
    
    if (!class_exists('WooCommerce')) {
        deactivate_plugins(plugin_basename(__FILE__));
        wp_die(
            esc_html('ContentGen AI Product Content & Image Generator requires WooCommerce to be installed and active.'),
            esc_html('Plugin Activation Error'),
            ['back_link' => true]
        );
    }
    
    $default_options = [
        'apcig_default_provider' => 'openai',
        'apcig_default_tone' => 'professional',
        'apcig_default_language' => 'en',
        'apcig_max_tokens' => 1000,
        'apcig_temperature' => 0.7,
        'apcig_enable_seo_optimization' => true,
        'apcig_enable_content_validation' => true,
        'apcig_enable_duplicate_detection' => true,
        'apcig_auto_apply_content' => false,
        'apcig_backup_original_content' => true,
        'apcig_data_retention_days' => 90
    ];
    
    foreach ($default_options as $option => $value) {
        if (get_option($option) === false) {
            update_option($option, $value);
        }
    }
    
    if (!wp_next_scheduled('apcig_cleanup_old_records')) {
        wp_schedule_event(time(), 'daily', 'apcig_cleanup_old_records');
    }
    
    if (!wp_next_scheduled('apcig_reset_daily_stats')) {
        wp_schedule_event(strtotime('tomorrow midnight'), 'daily', 'apcig_reset_daily_stats');
    }
    
    flush_rewrite_rules();
});

register_deactivation_hook(__FILE__, function() {
    wp_clear_scheduled_hook('apcig_cleanup_old_records');
    wp_clear_scheduled_hook('apcig_process_batch');
    wp_clear_scheduled_hook('apcig_reset_daily_stats');
    flush_rewrite_rules();
});