<?php
declare( strict_types=1 );

namespace FiboFilters\Integrations;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class BulkIntegrations
 * This class include all integrations that could be realized in bulk instead of creating separated files.
 */
class BulkIntegrations {

	public function init(): void {
		add_action( 'init', [ $this, 'product_brands_integration' ], 20 );
		add_action( 'init', [ $this, 'single_variations_integration' ], 20 );
		add_action( 'init', [ $this, 'canonical_links_integrations' ], 20 );
	}

	/**
	 * Enable integration with product brand third party tools.
	 */
	public function product_brands_integration(): void {
		add_filter(
			'fibofilters/filters/custom_sources/taxonomies',
			function ( $taxonomies ) {
				$brand_providers = $this->get_brand_providers_info();
				foreach ( $brand_providers as $brand_provider ) {
					$gen_label    = sprintf( _x( 'Brands (by %s)', '[admin]', 'fibofilters' ), $brand_provider['provider'] );
					$taxonomies[] = [
						/* translators: %s: plugin name that provides brands functionality  */
						'label'    => ! empty( $brand_provider['label'] ) ? $brand_provider['label'] : $gen_label,
						'taxonomy' => $brand_provider['taxonomy'],
					];
				}

				return $taxonomies;
			}
		);
	}

	/**
	 * Enable support for single variations.
	 */
	public function single_variations_integration(): void {
		global $WooCommerce_Single_Variations;

		$enabled = false;

		/**
		 *  Name: WooCommerce Show Single Variations by Iconic (v1.20.0)
		 *  Author: Iconic
		 *  URL: https://iconicwp.com/products/woocommerce-show-single-variations/
		 *  FF tested up to: v1.8.0
		 */
		if ( class_exists( 'Iconic_WSSV' ) && version_compare( \Iconic_WSSV::$version, '1.20.0' ) >= 0 ) {
			$enabled = true;
		}

		/**
		 *  Name: WPC Show Single Variations for WooCommerce (v2.4.2)
		 *  Author: WPClever
		 *  URL: https://wordpress.org/plugins/wpc-show-single-variations/
		 *  FF tested up to: v1.8.0
		 */
		if ( defined( 'WOOSV_VERSION' ) && version_compare( WOOSV_VERSION, '2.4.2' ) >= 0 ) {
			$enabled = true;
		}

		/**
		 *  Name: Variations as Single Product for WooCommerce (v3.5.0)
		 *  Author: StorePlugin
		 *  URL: https://wordpress.org/plugins/wc-variations-as-single-product/
		 *  FF tested up to: v1.8.0
		 */
		if ( defined( 'WC_VARIATIONS_AS_SINGLE_PRODUCT_VERSION' ) && version_compare( WC_VARIATIONS_AS_SINGLE_PRODUCT_VERSION, '3.5.0' ) >= 0 ) {
			$enabled = true;
		}

		/**
		 *  Name: WooCommerce Single Variations (v1.4.6)
		 *  Author: weLaunch
		 *  URL: https://welaunch.io/plugins/woocommerce-single-variations/
		 *  FF tested up to: v1.8.0
		 */
		if (
			// phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
			is_a( $WooCommerce_Single_Variations, 'WooCommerce_Single_Variations' ) &&
			is_callable( [ $WooCommerce_Single_Variations, 'get_version' ] ) &&
			version_compare( $WooCommerce_Single_Variations->get_version(), '1.4.6' ) >= 0
			// phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
		) {
			$enabled = true;
		}

		if ( $enabled ) {
			add_filter( 'fibofilters/config/single_variations_support', '__return_true' );
		}
	}

	/**
	 * Get information about taxonomy and plugin name of the current brand provider.
	 */
	private function get_brand_providers_info(): array {

		$providers = [];

		/**
		 *  Name: WooCommerce Core (v9.4)
		 *  Author: WooCommerce
		 *  URL: https://developer.woocommerce.com/2024/10/01/introducing-brands/
		 *
		 *  Starting with WooCommerce 9.4, all functionality previously offered by the WooCommerce Brands plugin
		 *  are part of WooCommerce core, and available to use for free.
		 *  The feature will be enabled for all users, starting with WooCommerce 9.6.
		 *
		 *  We also support previous plugin WooCommerce Brands (v1.6.61) (https://woocommerce.com/products/brands).
		 *
		 *  FF tested up to: v1.5.2.
		 */
		$woo_version       = defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '9.6', '>=' );
		$brands_enabled    = ( class_exists( 'Automattic\WooCommerce\Internal\Brands' )
							&& method_exists( 'Automattic\WooCommerce\Internal\Brands', 'is_enabled' )
							&& \Automattic\WooCommerce\Internal\Brands::is_enabled() );
		$enabled_in_core   = $woo_version || $brands_enabled;
		$enabled_in_plugin = defined( 'WC_BRANDS_VERSION' );

		if ( $enabled_in_core || $enabled_in_plugin ) {
			$gen_label   = sprintf( _x( 'Brands (by %s)', '[admin]', 'fibofilters' ), 'WooCommerce Brands' );
			$providers[] = [
				'label'    => $enabled_in_plugin ? $gen_label : 'Brands',
				'provider' => '',
				'taxonomy' => 'product_brand',
			];
		}

		/**
		 *  Name: Brands for WooCommerce (v3.8.2.3)
		 *  Author: BeRocket
		 *  URL: https://wordpress.org/plugins/brands-for-woocommerce/
		 *  FF tested up to: v1.0.0
		 */
		if ( defined( 'BeRocket_product_brand_version' ) ) {
			$providers[] = [
				'provider' => 'Brands for WooCommerce',
				'taxonomy' => 'berocket_brand',
			];
		}

		/**
		 *  Name: Perfect Brands WooCommerce (v3.2.8)
		 *  Author: QuadLayers
		 *  URL: https://wordpress.org/plugins/perfect-woocommerce-brands/
		 *  FF tested up to: v1.0.0
		 */
		if ( defined( 'PWB_PLUGIN_VERSION' ) ) {
			$providers[] = [
				'provider' => 'Perfect Brands WooCommerce',
				'taxonomy' => 'pwb-brand',
			];
		}

		/**
		 *  Name: Premmerce Brands for WooCommerce (v1.2.13)
		 *  Author: Premmerce
		 *  URL: https://wordpress.org/plugins/premmerce-woocommerce-brands/
		 *  FF tested up to: v1.0.0
		 */
		if ( class_exists( 'Premmerce\Brands\BrandsPlugin' ) ) {
			$providers[] = [
				'provider' => 'Premmerce Brands for WooCommerce',
				'taxonomy' => 'product_brand',
			];
		}

		/**
		 *  Name: YITH WooCommerce Brands Add-On (v2.17.0)
		 *  Author: YITH
		 *  URL: https://wordpress.org/plugins/yith-woocommerce-brands-add-on/
		 *  FF tested up to: v1.0.0
		 */
		if ( defined( 'YITH_WCBR_VERSION' ) ) {
			$providers[] = [
				'provider' => 'YITH WooCommerce Brands',
				'taxonomy' => 'yith_product_brand',
			];
		}

		/**
		 *  Name: Martfury Addons (v2.2.0)
		 *  Author: DrFuri
		 *  URL: https://drfuri.com/martfury
		 *  FF tested up to: v1.0.0
		 */
		if ( defined( 'MARTFURY_ADDONS_DIR' ) ) {
			$providers[] = [
				'provider' => 'Martfury Addons',
				'taxonomy' => 'product_brand',
			];
		}

		/**
		 *  Name: Smart Brands for WooCommerce (v1.0.7)
		 *  Author: ShapedPlugin
		 *  URL: https://wordpress.org/plugins/smart-brands-for-woocommerce/
		 *  FF tested up to: v1.0.0
		 */
		if ( defined( 'SMART_BRANDS_VERSION' ) ) {
			$providers[] = [
				'provider' => 'Smart Brands for WooCommerce',
				'taxonomy' => 'sp_smart_brand',
			];
		}

		/**
		 *  Name: WPC Brands for WooCommerce (v1.2.5)
		 *  Author: WPClever
		 *  URL: https://wordpress.org/plugins/wpc-brands/
		 *  FF tested up to: v1.0.0
		 */
		if ( defined( 'WPCBR_VERSION' ) ) {
			$providers[] = [
				'provider' => 'WPC Brands for WooCommerce',
				'taxonomy' => 'wpc-brand',
			];
		}

		/**
		 *  Name: Kadence Shop Kit (v2.0.28)
		 *  Author: Kadence WP
		 *  URL: https://www.kadencewp.com/kadence-shopkit/
		 *  FF tested up to: v1.3.0
		 */
		if ( defined( 'KADENCE_WOO_EXTRAS_VERSION' ) && taxonomy_exists( 'product_brands' ) ) {
			$providers[] = [
				'provider' => 'Kadence Shop Kit',
				'taxonomy' => 'product_brands',
			];
		}

		return apply_filters( 'fibofilters/filters/custom_sources/brand_providers', $providers );
	}

	/**
	 * Disable displaying canonical links to prevent duplicates
	 */
	public function canonical_links_integrations() {
		$disable_canonical = false;

		/**
		 *  Name: Rank Math SEO (v1.0.239)
		 *  Author: Rank Math SEO
		 *  URL: https://wordpress.org/plugins/seo-by-rank-math/
		 *  FF tested up to: v1.9.0
		 */
		if ( function_exists( 'rank_math' ) ) {
			$disable_canonical = true;
		}

		/**
		 *  Name: Yoast SEO (v24.8.1)
		 *  Author: Team Yoast
		 *  URL: https://wordpress.org/plugins/wordpress-seo/
		 *  FF tested up to: v1.9.0
		 */
		if ( defined( 'WPSEO_VERSION' ) ) {
			$disable_canonical = true;
		}

		if ( $disable_canonical ) {
			add_filter( 'fibofilters/print_canonical', '__return_false' );
		}
	}
}
