<?php
/**
 * EnableOwnProductPurchase class.
 *
 * @package DokanKits\Features\Vendor
 * @since   3.0.0
 */

namespace DokanKits\Features\Vendor;

use DokanKits\Features\Feature;
use WeDevs\Dokan\Product\Hooks;

/**
 * Enable Own Product Purchase Feature
 *
 * Allows vendors to purchase their own products by removing Dokan's
 * default restrictions. This enables vendors to test their own checkout
 * process or purchase their own products for personal use.
 * By default, Dokan prevents vendors from purchasing their own products.
 * This feature removes that restriction, enabling vendors to buy and review
 * their own products, which can be useful for testing or demonstration purposes.
 *
 * @since   3.0.0
 * @package DokanKits\Features\Vendor
 */
class EnableOwnProductPurchase extends Feature {

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

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

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

	/**
	 * 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 action to enable vendors to purchase their own products
	 * when this feature is enabled.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'init', array( $this, 'enable_purchase_if_enabled' ), 20 );
		add_filter( 'dokan_vendor_own_product_purchase_restriction', array( $this, 'allow_vendor_own_product_purchase' ), 999, 2 );
	}

	/**
	 * Enable own product purchase if feature is enabled
	 *
	 * Removes the default Dokan restrictions that prevent vendors from
	 * purchasing their own products when this feature is enabled.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function enable_purchase_if_enabled(): void {
		if ( ! $this->is_enabled() ) {
			return;
		}

		if ( ! function_exists( 'dokan_get_container' ) ) {
			return;
		}

		// Remove filters that restrict vendors from purchasing their own products.
		remove_action( 'woocommerce_before_single_product', array( dokan_get_container()->get( Hooks::class ), 'own_product_not_purchasable_notice' ) );
		remove_filter( 'woocommerce_is_purchasable', 'dokan_vendor_own_product_purchase_restriction' );
		remove_filter( 'woocommerce_product_review_comment_form_args', 'dokan_vendor_product_review_restriction' );
	}

	/**
	 * Keep own-product purchase allowed on Dokan 5+.
	 *
	 * @since 3.1.4
	 *
	 * @param bool       $is_purchasable Current purchasable flag.
	 * @param WC_Product $product        Product object.
	 *
	 * @return bool
	 */
	public function allow_vendor_own_product_purchase( bool $is_purchasable, $product ): bool {
		if ( ! $this->is_enabled() ) {
			return $is_purchasable;
		}

		return true;
	}
}
