<?php
/**
 * WooCommerce hooks: real-time product sync on changes.
 *
 * @package KwiroAISalesAgent
 */

defined( 'ABSPATH' ) || exit;

/**
 * Class Kwiro_Hooks
 *
 * Hooks into WooCommerce product lifecycle events to keep the cloud catalog
 * in sync. Handles product create/update/delete and stock changes.
 */
class Kwiro_Hooks {

	/**
	 * API Client instance.
	 *
	 * @var Kwiro_API_Client
	 */
	private $api_client;

	/**
	 * Product Sync instance.
	 *
	 * @var Kwiro_Product_Sync
	 */
	private $product_sync;

	/**
	 * IDs already synced this request (debounce).
	 *
	 * @var array
	 */
	private $synced_ids = array();

	/**
	 * Constructor.
	 *
	 * @param Kwiro_API_Client   $api_client   API client instance.
	 * @param Kwiro_Product_Sync $product_sync Product sync instance.
	 */
	public function __construct( $api_client, $product_sync ) {
		$this->api_client   = $api_client;
		$this->product_sync = $product_sync;

		$this->register_hooks();
	}

	/**
	 * Register WooCommerce hooks.
	 */
	private function register_hooks() {
		// Product save (create/update).
		add_action( 'woocommerce_update_product', array( $this, 'on_product_save' ), 10, 2 );
		add_action( 'woocommerce_new_product', array( $this, 'on_product_save' ), 10, 2 );

		// Product delete.
		add_action( 'before_delete_post', array( $this, 'on_product_delete' ) );
		add_action( 'wp_trash_post', array( $this, 'on_product_trash' ) );

		// Stock status changes.
		add_action( 'woocommerce_product_set_stock_status', array( $this, 'on_stock_change' ), 10, 3 );

		// Weekly full resync safety net.
		add_action( 'kwiro_weekly_full_sync', array( $this, 'weekly_full_sync' ) );
	}

	/**
	 * Handle product save (create or update).
	 *
	 * @param int        $product_id Product ID.
	 * @param WC_Product $product    Product object (may be null in some WC versions).
	 */
	public function on_product_save( $product_id, $product = null ) {
		// Debounce: skip if already synced this request.
		if ( in_array( $product_id, $this->synced_ids, true ) ) {
			return;
		}

		// Only sync published products.
		if ( ! $product ) {
			$product = wc_get_product( $product_id );
		}
		if ( ! $product || 'publish' !== $product->get_status() ) {
			return;
		}

		// Must be configured.
		if ( ! $this->api_client->is_configured() ) {
			return;
		}

		$formatted = $this->product_sync->format_product( $product );
		if ( ! $formatted ) {
			return;
		}

		$result = $this->api_client->post(
			'/api/sync/batch',
			array(
				'products' => array( $formatted ),
			)
		);

		if ( ! is_wp_error( $result ) ) {
			$this->synced_ids[] = $product_id;
		}
	}

	/**
	 * Handle product deletion.
	 *
	 * @param int $post_id Post ID.
	 */
	public function on_product_delete( $post_id ) {
		if ( 'product' !== get_post_type( $post_id ) ) {
			return;
		}

		if ( ! $this->api_client->is_configured() ) {
			return;
		}

		$this->api_client->post( '/api/sync/product/' . absint( $post_id ), array() );
	}

	/**
	 * Handle product trashing (treat as delete).
	 *
	 * @param int $post_id Post ID.
	 */
	public function on_product_trash( $post_id ) {
		$this->on_product_delete( $post_id );
	}

	/**
	 * Handle stock status changes.
	 *
	 * @param int        $product_id  Product ID.
	 * @param string     $stock_status New stock status.
	 * @param WC_Product $product     Product object.
	 */
	public function on_stock_change( $product_id, $stock_status, $product ) {
		// Re-sync the product with updated stock status.
		$this->on_product_save( $product_id, $product );
	}

	/**
	 * Weekly full resync to catch any missed changes.
	 */
	public function weekly_full_sync() {
		if ( ! $this->api_client->is_configured() ) {
			return;
		}

		$this->product_sync->trigger_full_sync();
	}

	/**
	 * Get synced IDs for this request (for testing).
	 *
	 * @return array
	 */
	public function get_synced_ids() {
		return $this->synced_ids;
	}

	/**
	 * Reset synced IDs (for testing).
	 */
	public function reset_synced_ids() {
		$this->synced_ids = array();
	}
}
