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

namespace DokanKits\Features\Vendor;

use DokanKits\Features\Feature;

/**
 * Remove Become Vendor Button Feature
 *
 * Removes the "Become a Vendor" button from the WooCommerce My Account page.
 * This feature is useful for marketplace owners who want to limit new vendor
 * registrations or control the vendor onboarding process through alternative means.
 *
 * @since   3.0.0
 * @package DokanKits\Features\Vendor
 */
class RemoveBecomeVendorButton extends Feature {

	/**
	 * Feature option key for enabling/disabling this feature
	 *
	 * @since 3.0.0
	 * @var string
	 */
	protected string $option_key = 'remove_become_a_vendor_button_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 = 'registration';

	/**
	 * 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 remove the "Become a Vendor" button
	 * when this feature is enabled.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'wp_loaded', array( $this, 'remove_button_if_enabled' ), 99999 );
	}

	/**
	 * Remove vendor button if feature is enabled
	 *
	 * Removes the "Become a Vendor" button from the WooCommerce
	 * My Account page by unhooking the relevant actions.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function remove_button_if_enabled(): void {
		if ( ! $this->is_enabled() ) {
			return;
		}

		if ( function_exists( 'dokan_get_container' ) ) {
			// Remove the shortcode if it exists.
			remove_shortcode( 'dokan-customer-migration' );

			// If Dokan plugin is active and the Become a Vendor feature exists.
			if ( dokan_get_container()->has( 'frontend_manager' ) ) {
				// Get the Become a Vendor instance.
				$become_a_vendor = dokan_get_container()->get( 'frontend_manager' )->become_a_vendor;

				// Remove actions related to the Become a Vendor feature.
				remove_action( 'template_redirect', array( $become_a_vendor, 'become_a_seller_form_handler' ) );
				remove_action( 'woocommerce_after_my_account', array( $become_a_vendor, 'render_become_a_vendor_section' ) );
				remove_action( 'woocommerce_account_account-migration_endpoint', array( $become_a_vendor, 'load_customer_to_vendor_update_template' ) );

				remove_action( 'init', array( $become_a_vendor, 'remove_account_update_feature_from_dokan_pro' ), 5 );
			}
		}
	}
}
