<?php
/**
 * Plugin Name: AISaleBot
 * Description: AI-powered sales chatbot integration for WordPress and WooCommerce.
 * Version: 1.0.4
 * Requires at least: 5.0
 * Requires PHP: 7.2
 * WC requires at least: 8.0
 * WC tested up to: 10.9
 * Author: AI Brain Solution
 * Author URI: https://aisalebot.io/
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: aisalebot
 */

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

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
            );
        }
    }
);

define( 'AICSAPP_PATH', plugin_dir_path( __FILE__ ) );
define( 'AICSAPP_URL', plugin_dir_url( __FILE__ ) );
define( 'AICSAPP_VER', '1.0.3' );

require_once AICSAPP_PATH . 'includes/Admin.php';
require_once AICSAPP_PATH . 'includes/Data.php';
require_once AICSAPP_PATH . 'includes/Helpers.php';

// Init admin
\AICSAPP\Admin::init();

/**
 * Frontend loader
 * - Fixed script source
 * - User provides Chatbot ID
 * - Loads only when enabled
 */
add_action( 'wp_footer', function () {

    $enabled    = (int) get_option( 'aicsapp_enabled', 0 );
    $chatbot_id = get_option( 'aicsapp_chatbot_id' );

    if ( ! $enabled || empty( $chatbot_id ) ) {
        return;
    }
    ?>
    <!-- AISaleBot -->
    <script
        src="https://aisalebot.io/chatbot_widget/chatbot-widget.js"
        data-id="<?php echo esc_attr( $chatbot_id ); ?>">
    </script>
    <!-- /AISaleBot -->
    <?php
}, 9999 );
