<?php

namespace Fotogenic_AI\Compat\WooCommerce;

use Fotogenic_AI\Bulk_Cache_Version;

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

/**
 * Registers WooCommerce hooks that invalidate bulk totals/categories caches.
 */
final class WooCommerce_Bulk_Cache_Invalidation {

    private static bool $registered = false;

    public function register(): void {
        if ( self::$registered ) {
            return;
        }
        add_action( 'save_post_product', [ $this, 'on_save_product' ], 10, 2 );
        add_action( 'deleted_post', [ $this, 'on_deleted_post' ], 10, 1 );
        add_action( 'trashed_post', [ $this, 'on_trashed_post' ] );
        add_action( 'untrashed_post', [ $this, 'on_untrashed_post' ] );
        add_action( 'transition_post_status', [ $this, 'on_transition_post_status' ], 10, 3 );
        add_action( 'set_object_terms', [ $this, 'on_set_object_terms' ], 10, 6 );

        add_action( 'created_product_cat', [ $this, 'on_product_cat_change' ] );
        add_action( 'edited_product_cat', [ $this, 'on_product_cat_change' ] );
        add_action( 'delete_product_cat', [ $this, 'on_product_cat_change' ] );

        add_action( 'woocommerce_product_set_stock', [ $this, 'on_stock_change' ], 10, 1 );
        add_action( 'woocommerce_product_set_stock_status', [ $this, 'on_stock_change' ], 10, 2 );
        add_action( 'woocommerce_variation_set_stock', [ $this, 'on_stock_change' ], 10, 1 );
        add_action( 'woocommerce_variation_set_stock_status', [ $this, 'on_stock_change' ], 10, 2 );

        self::$registered = true;
    }

    public function on_save_product( int $post_id, \WP_Post $post ): void {
        if ( 'product' !== ( $post->post_type ?? '' ) ) {
            return;
        }
        $this->bump();
    }

    public function on_deleted_post( int $post_id ): void {
        $post = get_post( $post_id );
        if ( $post && 'product' === ( $post->post_type ?? '' ) ) {
            $this->bump();
        }
    }

    public function on_trashed_post( int $post_id ): void {
        $post = get_post( $post_id );
        if ( $post && 'product' === ( $post->post_type ?? '' ) ) {
            $this->bump();
        }
    }

    public function on_untrashed_post( int $post_id ): void {
        $post = get_post( $post_id );
        if ( $post && 'product' === ( $post->post_type ?? '' ) ) {
            $this->bump();
        }
    }

    public function on_transition_post_status( string $new_status, string $old_status, \WP_Post $post ): void {
        if ( 'product' !== ( $post->post_type ?? '' ) ) {
            return;
        }
        if ( 'publish' === $new_status || 'publish' === $old_status ) {
            $this->bump();
        }
    }

    public function on_set_object_terms( int $object_id, $terms, array $tt_ids, string $taxonomy, bool $append, array $old_tt_ids ): void {
        if ( 'product_cat' !== $taxonomy ) {
            return;
        }
        $post = get_post( $object_id );
        if ( $post && 'product' === ( $post->post_type ?? '' ) ) {
            $this->bump();
        }
    }

    public function on_product_cat_change(): void {
        $this->bump();
    }

    public function on_stock_change(): void {
        $this->bump();
    }

    private function bump(): void {
        Bulk_Cache_Version::bump();
    }
}
