<?php
/**
 * WooCommerce Integration Class
 *
 * Integrates RedFox Tab Manager with WooCommerce frontend product tabs.
 *
 * @package RedFoxWP\RedFoxTabManager
 * @since 1.0.0
 */

namespace RedFoxWP\RedFoxTabManager;

use RedFoxWP\RedFoxTabManager\Abstracts\Abstract_Singleton;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class WooCommerce_Integration
 *
 * Handles WooCommerce frontend product tab integration for RedFox Tabs.
 *
 * @since 1.0.0
 */
class WooCommerce_Integration extends Abstract_Singleton {

	/**
	 * Constructor
	 *
	 * @since 1.0.0
	 * @access protected
	 *
	 * @return void
	 */
	protected function __construct() {
		// Only initialize if WooCommerce is active.
		if ( ! $this->is_woocommerce_active() ) {
			return;
		}

		$this->init_hooks();
	}

	/**
	 * Check if WooCommerce is active
	 *
	 * @since 1.0.0
	 * @access private
	 *
	 * @return bool
	 */
	private function is_woocommerce_active() {
		return class_exists( 'WooCommerce' );
	}

	/**
	 * Initialize WordPress hooks
	 *
	 * @since 1.0.0
	 * @access private
	 *
	 * @return void
	 */
	private function init_hooks() {
		// Frontend display.
		add_filter( 'woocommerce_product_tabs', array( $this, 'add_frontend_product_tabs' ) );
	}



	/**
	 * Add global tabs to product display
	 *
	 * @param array $tabs Existing product tabs.
	 * @return array Modified tabs.
	 * @since 1.0.0
	 * @access private
	 */
	private function add_global_tabs_to_product( $tabs ) {
		$admin_global_tabs = Global_Tabs::get_instance();
		$global_tabs       = $admin_global_tabs->get_global_tabs();

		if ( ! is_array( $global_tabs ) ) {
			return $tabs;
		}

		// Process global tabs in their admin-defined order.
		// Use index-based priority calculation: 10 * $index.
		foreach ( $global_tabs as $index => $global_tab ) {
			// Handle different tab types.
			if ( 'woocommerce_default' === ( $global_tab['type'] ?? '' ) ) {
				// Handle WooCommerce default tabs.
				if ( ! ( $global_tab['enabled'] ?? false ) ) {
					// Remove disabled WooCommerce default tabs.
					unset( $tabs[ $global_tab['id'] ?? '' ] );
				} elseif ( isset( $tabs[ $global_tab['id'] ?? '' ] ) ) {
					// Update priority of enabled WooCommerce default tabs.
					$tabs[ $global_tab['id'] ]['priority'] = ( 10 * $index ) + 10;
				}
				continue;
			}

			// Skip disabled non-WooCommerce tabs.
			if ( ! ( $global_tab['enabled'] ?? false ) ) {
				continue;
			}

			if ( 'redfoxwp_tab' === ( $global_tab['type'] ?? '' ) && ! empty( $global_tab['redfoxwp_tab_id'] ?? '' ) ) {
				// Handle RedFox CPT tabs.
				$tab_post = get_post( $global_tab['redfoxwp_tab_id'] );
				if ( ! $tab_post || Tabs_CPT::CPT_NAME !== $tab_post->post_type ) {
					continue;
				}

				$tabs[ 'redfox_tab_manager_global_tab_' . ( $global_tab['id'] ?? uniqid() ) ] = array(
					'title'    => $tab_post->post_title,
					'priority' => ( 10 * $index ) + 10, // Use 10 * index for priority calculation.
					'callback' => array( $this, 'render_frontend_tab_content' ),
					'content'  => $tab_post->post_content,
				);
			} else {
				// Handle custom global tabs (with inline content).
				$tabs[ 'redfox_tab_manager_global_tab_' . ( $global_tab['id'] ?? uniqid() ) ] = array(
					'title'    => $global_tab['title'] ?? '',
					'priority' => ( 10 * $index ) + 10, // Use 10 * index for priority calculation.
					'callback' => array( $this, 'render_frontend_tab_content' ),
					'content'  => $global_tab['content'] ?? '',
				);
			}
		}

		return $tabs;
	}

	/**
	 * Add global tabs to frontend product display
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param array $tabs Existing product tabs.
	 * @return array Modified tabs.
	 */
	public function add_frontend_product_tabs( $tabs ) {
		global $product;

		if ( ! $product ) {
			return $tabs;
		}

		// Add global tabs.
		$tabs = $this->add_global_tabs_to_product( $tabs );

		return $tabs;
	}

	/**
	 * Render frontend tab content
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param string $key Tab key.
	 * @param array  $tab Tab data.
	 * @return void
	 */
	public function render_frontend_tab_content( $key, $tab ) {
		$content = apply_filters( 'the_content', $tab['content'] ?? '' );
		echo '<div class="redfox-tab-content">';
		echo wp_kses_post( $content );
		echo '</div>';
	}
}
