<?php
/**
 * HideAddToCartButton class.
 *
 * @package DokanKits\Features\Product
 * @since   3.0.0
 */

namespace DokanKits\Features\Product;

use DokanKits\Features\Feature;

/**
 * Hide Add to Cart Button Feature
 *
 * Hides the Add to Cart button from WooCommerce product pages and archive listings.
 * This feature removes both the default buttons and adds CSS to hide any remaining buttons.
 *
 * @since   3.0.0
 * @package DokanKits\Features\Product
 */
class HideAddToCartButton extends Feature {

	/**
	 * Feature option key for enabling/disabling this feature
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $option_key = 'hide_add_to_cart_button_checkbox';

	/**
	 * Feature settings group for admin UI organization
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $settings_group = 'cart';

	/**
	 * Feature settings section within the group
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $settings_section = 'buttons';

	/**
	 * Number of sub-features contained in this feature
	 *
	 * @since 3.0.0
	 * @var int
	 */
	protected int $total_features = 1;

	/**
	 * Register hooks with WordPress
	 *
	 * Sets up the necessary actions to hide add to cart buttons when the feature is enabled.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'init', array( $this, 'hide_buttons_if_enabled' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'add_css_if_enabled' ) );
	}

	/**
	 * Hide add to cart buttons if feature is enabled
	 *
	 * Removes the standard WooCommerce add to cart buttons from product loops
	 * and single product pages.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function hide_buttons_if_enabled(): void {
		if ( ! $this->is_enabled() ) {
			return;
		}

		// Remove standard WooCommerce add to cart buttons.
		remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
		remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
	}

	/**
	 * Add CSS to hide any remaining buttons if feature is enabled
	 *
	 * Adds inline CSS to hide add to cart buttons that couldn't be removed
	 * through hook removal.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function add_css_if_enabled(): void {
		if ( ! $this->is_enabled() ) {
			return;
		}

		wp_add_inline_style(
			'woocommerce-general',
			'
			.single-product .product-type-simple .single_add_to_cart_button,
			.single-product .product-type-variable .single_add_to_cart_button,
			.product .add_to_cart_button {
				display: none !important;
			}
		'
		);
	}
}
