<?php
/**
 * Remove Other Options Feature
 *
 * @package DokanKits\Features\Product
 */

namespace DokanKits\Features\Product;

use DokanKits\Features\Feature;

/**
 * Remove Other Options Feature
 *
 * Controls the visibility of the "Other Options" section in the Dokan edit product form.
 * This feature allows site administrators to hide the other options section in the product
 * edit form for vendors, simplifying the UI and focusing vendors on essential fields.
 *
 * @since   3.0.0
 * @package DokanKits\Features\Product
 */
class ProductRemoveOtherOptions extends Feature {

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

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

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

	/**
	 * Total number of 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 the other options section
	 * in both admin and frontend product edit forms.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_action( 'wp_head', array( $this, 'add_inline_styles' ) );
		add_action( 'admin_head', array( $this, 'add_inline_styles' ) );
		add_filter( 'dokan_kits_template_classes', array( $this, 'add_body_class' ) );
	}

	/**
	 * Add inline CSS to hide the other options section
	 *
	 * Outputs CSS styles to hide the "Other Options" section in the
	 * Dokan product edit form when enabled.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function add_inline_styles(): void {
		if ( ! $this->is_enabled() ) {
			return;
		}

		// Only apply on frontend Dokan pages or admin edit product page.
		if ( ! $this->should_apply_styles() ) {
			return;
		}

		echo '
			<style type="text/css">
				.dokan-product-edit-form .dokan-other-options { 
					display: none !important; 
				}
			</style>
		';
	}

	/**
	 * Check if we should apply styles on current page
	 *
	 * Determines whether the current page is a valid context for
	 * applying the CSS that hides the other options section.
	 *
	 * @since 3.0.0
	 * @return bool Whether styles should be applied.
	 */
	protected function should_apply_styles(): bool {
		global $pagenow;

		// Apply on Dokan frontend pages.
		if ( function_exists( 'dokan_is_seller_dashboard' ) && dokan_is_seller_dashboard() ) {
			return true;
		}

		// Apply on WP admin product edit pages.
		if ( is_admin() && in_array( $pagenow, array( 'post.php', 'post-new.php' ), true ) && get_post_type() === 'product' ) {
			return true;
		}

		return false;
	}

	/**
	 * Add body class for template modifications
	 *
	 * Adds a CSS class to the body when this feature is enabled to allow
	 * template-level adjustments if needed.
	 *
	 * @since 3.0.0
	 * @param array $classes Existing body classes.
	 * @return array Modified body classes.
	 */
	public function add_body_class( array $classes ): array {
		if ( $this->is_enabled() ) {
			$classes[] = 'dokan-hide-other-options';
		}
		return $classes;
	}
}
