<?php
/**
 * Plugin Name: Voice Logica AI for WooCommerce
 * Description: Connect your WooCommerce store to Voice Logica AI Voice Agents. Enable voice-powered order creation, product search, customer lookup, and order history—so your customers get 24/7 voice support.
 * Version: 1.0.0
 * Author: VoiceLogica
 * Author URI: https://www.voicelogica.ai
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: voicelogica-ai-for-woocommerce
 * Requires at least: 5.0
 * Requires PHP: 7.4
 * Requires Plugins: woocommerce
 * WC requires at least: 5.0
 * WC tested up to: 8.0
 *
 * @package VoiceLogica_AI_WooCommerce
 */

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

// Define plugin constants.
define( 'VOICELOGICA_AI_WC_VERSION', '1.0.0' );
define( 'VOICELOGICA_AI_WC_PLUGIN_FILE', __FILE__ );
define( 'VOICELOGICA_AI_WC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'VOICELOGICA_AI_WC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'VOICELOGICA_AI_WC_WEBHOOK_URL', 'https://api.voicelogica.ai/api/woocommerce' );

add_action( 'before_woocommerce_init', 'voicelogica_ai_declare_hpos_compatibility' );

/**
 * Declare HPOS (High-Performance Order Storage) compatibility to WooCommerce.
 * Must run before WooCommerce init to be recognized.
 *
 * @since 1.0.0
 */
function voicelogica_ai_declare_hpos_compatibility() {
	if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
	}
}

/**
 * Main plugin class
 */
class VoiceLogica_AI_WooCommerce {

	/**
	 * Plugin instance
	 *
	 * @var VoiceLogica_AI_WooCommerce
	 */
	private static $instance = null;

	/**
	 * Get plugin instance
	 *
	 * @return VoiceLogica_AI_WooCommerce
	 */
	public static function get_instance() {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

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

	/**
	 * Initialize plugin
	 */
	private function init() {
		// Load plugin files.
		$this->load_dependencies();

		// Initialize plugin components.
		add_action( 'plugins_loaded', array( $this, 'load_plugin' ) );
	}

	/**
	 * Load plugin dependencies
	 */
	private function load_dependencies() {
		require_once VOICELOGICA_AI_WC_PLUGIN_DIR . 'includes/class-bootstrap.php';
		require_once VOICELOGICA_AI_WC_PLUGIN_DIR . 'includes/class-settings.php';
		require_once VOICELOGICA_AI_WC_PLUGIN_DIR . 'includes/class-api-client.php';
		require_once VOICELOGICA_AI_WC_PLUGIN_DIR . 'includes/class-privacy.php';
		require_once VOICELOGICA_AI_WC_PLUGIN_DIR . 'includes/class-rest-customer-phone.php';
	}

	/**
	 * Load plugin after WordPress and WooCommerce are loaded
	 */
	public function load_plugin() {
		// Check if WooCommerce is active.
		if ( ! class_exists( 'WooCommerce' ) ) {
			add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
			return;
		}

		// Initialize plugin components.
		new VoiceLogica_AI_Bootstrap();
	}

	/**
	 * WooCommerce missing notice
	 */
	public function woocommerce_missing_notice() {
		?>
		<div class="error">
			<p><?php esc_html_e( 'Voice Logica AI for WooCommerce requires WooCommerce to be installed and active.', 'voicelogica-ai-for-woocommerce' ); ?></p>
		</div>
		<?php
	}
}

/**
 * Initialize the plugin.
 */
function voicelogica_ai_wc_init() {
	return VoiceLogica_AI_WooCommerce::get_instance();
}

// Start the plugin.
voicelogica_ai_wc_init();
