<?php
/**
 * WooCommerce integration
 *
 * @package BubblaV_Chat
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * WooCommerce class
 */
class Bubblav_WooCommerce {

	/**
	 * Initialize WooCommerce integration
	 *
	 * @return void
	 */
	public function init_integration() {
		// Enable ecommerce flag.
		update_option( 'bubblav_is_woocommerce', '1' );

		// Add WooCommerce-specific hooks.
		add_action( 'woocommerce_new_product', array( $this, 'on_product_created' ), 10, 1 );
		add_action( 'woocommerce_product_updated', array( $this, 'on_product_updated' ), 10, 1 );

		// Add product data to widget config.
		add_filter( 'bubblav_widget_config', array( $this, 'add_product_context' ) );
	}

	/**
	 * Handle new product creation
	 *
	 * @param int $product_id Product ID.
	 * @return void
	 */
	public function on_product_created( $product_id ) {
		// This is a placeholder for future product sync functionality.
		// For MVP, we only detect WooCommerce presence.
		// Product sync will be added in a future version.

		/**
		 * Fires when a new product is created in WooCommerce
		 * Use this hook to implement custom product sync functionality
		 *
		 * @param int $product_id Product ID.
		 */
		do_action( 'bubblav_woocommerce_product_created', $product_id );
	}

	/**
	 * Handle product updates
	 *
	 * @param int $product_id Product ID.
	 * @return void
	 */
	public function on_product_updated( $product_id ) {
		// This is a placeholder for future product sync functionality.
		// For MVP, we only detect WooCommerce presence.
		// Product sync will be added in a future version.

		/**
		 * Fires when a product is updated in WooCommerce
		 * Use this hook to implement custom product sync functionality
		 *
		 * @param int $product_id Product ID.
		 */
		do_action( 'bubblav_woocommerce_product_updated', $product_id );
	}

	/**
	 * Add product context to widget config.
	 *
	 * @param array $config Widget config.
	 * @return array Modified widget config.
	 */
	public function add_product_context( $config ) {
		if ( ! is_product() ) {
			return $config;
		}

		global $product;

		if ( ! $product ) {
			return $config;
		}

		// Add product context for single product pages.
		$config['product'] = array(
			'id'           => $product->get_id(),
			'name'         => $product->get_name(),
			'price'        => $product->get_price(),
			'stock_status' => $product->get_stock_status(),
		);

		return $config;
	}

	/**
	 * Check if WooCommerce HPOS is enabled
	 *
	 * @return bool True if HPOS is enabled
	 */
	public function is_hpos_enabled() {
		if ( ! class_exists( 'WooCommerce' ) ) {
			return false;
		}

		return wc_get_container()->get( \Automattic\WooCommerce\Internal\DataStores\Orders\DataSynchronizer::class )->custom_orders_table_usage_is_enabled();
	}

	/**
	 * Get store currency
	 *
	 * @return string Store currency code
	 */
	public function get_store_currency() {
		return get_woocommerce_currency();
	}

	/**
	 * Get store currency symbol
	 *
	 * @return string Store currency symbol
	 */
	public function get_currency_symbol() {
		return get_woocommerce_currency_symbol();
	}
}
