<?php
/**
 * Cache Sync Hooks
 * 
 * Registers WooCommerce hooks for automatic cache synchronization
 * 
 * @package VariationHub
 * @since 1.0.0
 */

namespace VariationHub\Core;

defined('ABSPATH') || exit;

class Cache_Hooks {
    
    /**
     * Initialize hooks
     */
    public static function init() {
        // Variation created
        add_action('woocommerce_new_product_variation', [__CLASS__, 'on_variation_saved'], 10, 1);
        
        // Variation updated
        add_action('woocommerce_update_product_variation', [__CLASS__, 'on_variation_saved'], 10, 1);
        
        // Variation deleted
        add_action('woocommerce_delete_product_variation', [__CLASS__, 'on_variation_deleted'], 10, 1);
        
        // Parent product updated (may affect variation attributes/names)
        add_action('woocommerce_update_product', [__CLASS__, 'on_parent_saved'], 10, 1);
        
        // Generic save_post hook (catch-all for external plugins)
        add_action('save_post_product_variation', [__CLASS__, 'on_post_saved'], 20, 3);
        
        // Bulk actions
        add_action('woocommerce_variable_product_bulk_edit_save', [__CLASS__, 'on_bulk_edit'], 10, 1);
        
        // Stock updated (WooCommerce stock management)
        add_action('woocommerce_variation_set_stock', [__CLASS__, 'on_stock_changed'], 10, 1);
        
        // WP All Import compatibility
        add_action('pmxi_saved_post', [__CLASS__, 'on_import_saved'], 10, 2);
        
        // Action Scheduler batch sync
        add_action('varihu_cache_sync_batch', [Cache_Sync::class, 'process_batch_sync'], 10, 1);
    }
    
    /**
     * Variation saved (created or updated)
     * 
     * @param int $variation_id
     */
    public static function on_variation_saved($variation_id) {
        if (!Cache_Table::table_exists()) {
            return;
        }
        
        Cache_Sync::sync_variation($variation_id);
    }
    
    /**
     * Variation deleted
     * 
     * @param int $variation_id
     */
    public static function on_variation_deleted($variation_id) {
        if (!Cache_Table::table_exists()) {
            return;
        }
        
        Cache_Sync::delete_variation($variation_id);
    }
    
    /**
     * Parent product saved (update all variations)
     * 
     * @param int $product_id
     */
    public static function on_parent_saved($product_id) {
        if (!Cache_Table::table_exists()) {
            return;
        }
        
        $product = wc_get_product($product_id);
        
        if ($product && $product->is_type('variable')) {
            // Re-sync all variations (parent name or attributes may have changed)
            Cache_Sync::sync_parent_variations($product_id);
        }
    }
    
    /**
     * Generic save_post hook (catch external updates)
     * 
     * @param int $post_id
     * @param \WP_Post $post
     * @param bool $update
     */
    public static function on_post_saved($post_id, $post, $update) {
        if (!Cache_Table::table_exists()) {
            return;
        }
        
        // Skip auto-saves and revisions
        if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
            return;
        }
        
        // Only process published variations
        if ($post->post_type === 'product_variation' && $post->post_status === 'publish') {
            Cache_Sync::sync_variation($post_id);
        }
    }
    
    /**
     * Bulk edit action
     * 
     * @param \WC_Product $product
     */
    public static function on_bulk_edit($product) {
        if (!Cache_Table::table_exists()) {
            return;
        }
        
        if ($product && $product->is_type('variable')) {
            Cache_Sync::sync_parent_variations($product->get_id());
        }
    }
    
    /**
     * Stock changed
     * 
     * @param \WC_Product_Variation $variation
     */
    public static function on_stock_changed($variation) {
        if (!Cache_Table::table_exists()) {
            return;
        }
        
        Cache_Sync::sync_variation($variation->get_id());
    }
    
    /**
     * WP All Import saved post
     * 
     * @param int $post_id
     * @param \SimpleXMLElement $xml_node
     */
    public static function on_import_saved($post_id, $xml_node) {
        if (!Cache_Table::table_exists()) {
            return;
        }
        
        if (get_post_type($post_id) === 'product_variation') {
            // Delay sync to ensure all meta is saved
            wp_schedule_single_event(time() + 5, 'varihu_sync_imported_variation', [$post_id]);
        }
    }
}
