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

namespace DokanKits\Features\Product;

use DokanKits\Features\Feature;

/**
 * Product Types Feature
 *
 * Manages which product types are available to vendors in the Dokan marketplace.
 * This feature allows site administrators to selectively disable specific
 * WooCommerce product types for vendors to simplify the interface or restrict
 * capabilities based on business needs.
 *
 * @since   3.0.0
 * @package DokanKits\Features\Product
 */
class ProductTypes extends Feature {

	/**
	 * 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 = 'types';

	/**
	 * Available product types from WooCommerce
	 *
	 * @since 3.0.0
	 * @var array
	 */
	protected array $product_types = array();

	/**
	 * Register hooks with WordPress
	 *
	 * Adds filter to modify the available product types in Dokan
	 * based on the configured settings.
	 *
	 * @since 3.0.0
	 * @return void
	 */
	public function register_hooks(): void {
		add_filter( 'dokan_product_types', array( $this, 'filter_product_types' ), 20 );
	}

	/**
	 * Filter product types based on settings
	 *
	 * Removes product types that have been disabled in the settings
	 * from the list of available product types for vendors.
	 *
	 * @since 3.0.0
	 * @param array $product_types Current product types available in Dokan.
	 * @return array Filtered product types.
	 */
	public function filter_product_types( array $product_types ): array {
		// Only modify if at least one of the options is enabled.
		if ( ! $this->is_enabled() ) {
			return $product_types;
		}

		// Check each product type option and remove if disabled.
		foreach ( $this->get_product_types() as $product_type ) {
			if ( ! isset( $product_types[ $product_type ] ) ) {
				continue;
			}

			if ( $this->is_product_type_disabled( $product_type ) ) {
				unset( $product_types[ $product_type ] );
			}
		}

		return $product_types;
	}

	/**
	 * Check if the feature is enabled for any product type
	 *
	 * Overrides the parent is_enabled() method to check if any individual
	 * product type is disabled, rather than using a single option key.
	 *
	 * @since 3.0.0
	 * @return bool True if at least one product type is disabled via settings.
	 */
	public function is_enabled(): bool {
		foreach ( $this->get_product_types() as $product_type ) {
			if ( $this->is_product_type_disabled( $product_type ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Check if a specific product type is disabled via settings
	 *
	 * @since 3.0.0
	 * @param string $product_type Product type key.
	 * @return bool True if the product type is disabled.
	 */
	protected function is_product_type_disabled( string $product_type ): bool {
		return wc_string_to_bool( $this->get_setting( 'remove_' . $product_type . '_product_checkbox', false ) );
	}

	/**
	 * Get available product types
	 *
	 * Returns an array of all WooCommerce product types that can be
	 * configured in the Dokan marketplace.
	 *
	 * @since 3.0.0
	 * @return array List of product type keys.
	 */
	public function get_product_types(): array {
		if ( count( $this->product_types ) === 0 ) {
			$this->product_types = array_keys( wc_get_product_types() );
		}

		/**
		 * Filter the available product types.
		 *
		 * @since 3.0.0
		 *
		 * @param array $product_types Available product types.
		 */
		return apply_filters( 'dokan_kits_product_types', $this->product_types );
	}

	/**
	 * Get total number of product types
	 *
	 * Returns the count of all available product types that can be
	 * configured in the Dokan marketplace.
	 *
	 * @since 3.0.0
	 * @return int Total number of product types.
	 */
	public function get_total_features(): int {
		return count( $this->get_product_types() );
	}

	/**
	 * Get active feature count
	 *
	 * Returns the number of active features within this feature class.
	 * For ProductTypes, this counts how many product types are being disabled.
	 *
	 * @since 3.0.0
	 * @return int Number of disabled product types.
	 */
	public function get_active_features_count(): int {
		$count = 0;

		foreach ( $this->get_product_types() as $product_type ) {
			if ( $this->is_product_type_disabled( $product_type ) ) {
				++$count;
			}
		}

		return $count;
	}
}
