<?php
/**
 * Plugin Name: ReplyIt Chat Widget
 * Plugin URI: https://replyit.ai/docs/wordpress-plugin
 * Description: AI-powered chat widget with WooCommerce integration for customer support and sales automation
 * Version: 1.0.2
 * Author: ReplyIt
 * Author URI: https://replyit.ai
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: replyit-chat-widget
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.4
 * WC requires at least: 5.0
 * WC tested up to: 8.5
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Define plugin constants
define('REPLYIT_CHAT_VERSION', '1.0.2');
define('REPLYIT_CHAT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('REPLYIT_CHAT_PLUGIN_URL', plugin_dir_url(__FILE__));
define('REPLYIT_CHAT_PLUGIN_FILE', __FILE__);

/**
 * Main Plugin Class
 */
class ReplyIt_Chat_Widget {

    /**
     * Single instance of the class
     */
    private static $instance = null;

    /**
     * Get single instance
     */
    public static function instance() {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor
     */
    private function __construct() {
        $this->load_dependencies();
        $this->init_hooks();
    }

    /**
     * Load required files
     */
    private function load_dependencies() {
        require_once REPLYIT_CHAT_PLUGIN_DIR . 'includes/class-replyit-core.php';
        require_once REPLYIT_CHAT_PLUGIN_DIR . 'includes/class-replyit-admin.php';
        require_once REPLYIT_CHAT_PLUGIN_DIR . 'includes/class-replyit-frontend.php';
        require_once REPLYIT_CHAT_PLUGIN_DIR . 'includes/class-replyit-api.php';
        require_once REPLYIT_CHAT_PLUGIN_DIR . 'includes/class-replyit-security.php';
        require_once REPLYIT_CHAT_PLUGIN_DIR . 'includes/class-replyit-rest-api.php';
    }

    /**
     * Initialize hooks
     */
    private function init_hooks() {
        register_activation_hook(REPLYIT_CHAT_PLUGIN_FILE, [$this, 'activate']);
        register_deactivation_hook(REPLYIT_CHAT_PLUGIN_FILE, [$this, 'deactivate']);

        add_action('init', [$this, 'init']);
    }

    /**
     * Plugin activation
     */
    public function activate() {
        // Create options with default values
        add_option('replyit_widget_id', '');
        add_option('replyit_api_secret', '');
        add_option('replyit_api_url', 'https://replyit.ai');
        add_option('replyit_enabled', true);
        add_option('replyit_woocommerce_enabled', class_exists('WooCommerce'));

        // Auto-register domain (will be done on first settings save)
        flush_rewrite_rules();
    }

    /**
     * Plugin deactivation
     */
    public function deactivate() {
        flush_rewrite_rules();
    }

    /**
     * Initialize plugin components
     */
    public function init() {
        // Initialize core components
        ReplyIt_Core::instance();
        ReplyIt_Admin::instance();
        ReplyIt_Frontend::instance();
        ReplyIt_API::instance();
        ReplyIt_REST_API::instance();

        // Always load context and blog classes (works for all WordPress sites)
        require_once REPLYIT_CHAT_PLUGIN_DIR . 'includes/class-replyit-context.php';
        require_once REPLYIT_CHAT_PLUGIN_DIR . 'includes/class-replyit-blog.php';

        // Initialize context and blog
        ReplyIt_Context::instance();
        ReplyIt_Blog::instance();

        // Load and initialize WooCommerce integration if available
        if (class_exists('WooCommerce')) {
            // Load WooCommerce class
            require_once REPLYIT_CHAT_PLUGIN_DIR . 'includes/class-replyit-woocommerce.php';

            // Initialize if enabled
            if (get_option('replyit_woocommerce_enabled')) {
                ReplyIt_WooCommerce::instance();
            }
        }
    }
}

/**
 * Initialize the plugin
 */
function replyit_chat_widget() {
    return ReplyIt_Chat_Widget::instance();
}

// Start the plugin
replyit_chat_widget();
