<?php
/**
 * File Path: includes/helpers/class-mbs-color-vars.php
 *
 * My Book Showroom — CSS Custom Property Output
 *
 * Outputs the :root .mbs-wrapper { --mbs-* } block on MBS content pages.
 *
 * FREE tier: 4 tokens (accent, button-bg, button-text, link).
 * PRO tier:  20 tokens (FREE set + 16 extended palette tokens).
 *
 * Hooked to wp_head at priority 1 by MBS_Plugin::init().
 * Only fires on pages that actually contain MBS content — the same
 * context gate used by MBS_Plugin::enqueue_front_assets() — so the
 * style block and its get_option() calls never run on pages without
 * any MBS shortcodes, post type archives, or taxonomy templates.
 *
 * Moved from bookshowroom.php (D-2 / REF-02) to remove output logic
 * from the bootstrap file and add the missing page-context gate.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

class MBS_Color_Vars {

	/**
	 * Register the wp_head hook.
	 *
	 * Called once from MBS_Plugin::init(). Guards against double-registration.
	 *
	 * @return void
	 */
	public static function register(): void {
		if ( ! has_action( 'wp_head', array( __CLASS__, 'output' ) ) ) {
			add_action( 'wp_head', array( __CLASS__, 'output' ), 1 );
		}
	}

	/**
	 * Determine whether the current page contains MBS content.
	 *
	 * Mirrors the context check in MBS_Plugin::enqueue_front_assets() so
	 * the style block is only emitted when MBS assets are also present.
	 *
	 * @return bool
	 */
	private static function current_page_has_mbs_content(): bool {
		if ( is_singular( MBS_POST_TYPE ) ) {
			return true;
		}
		if ( is_post_type_archive( MBS_POST_TYPE ) ) {
			return true;
		}
		if ( is_tax( array( MBS_TAX_AUTHOR, MBS_TAX_SERIES, MBS_TAX_GENRE, MBS_TAX_COLLECTION, MBS_TAX_UNIVERSE, MBS_TAX_TAG, MBS_TAX_THEME, MBS_TAX_CONTENT_WARNING, MBS_TAX_PUBLISHER, MBS_TAX_IMPRINT, MBS_TAX_CONTRIBUTOR ) ) ) {
			return true;
		}

		global $post;
		if ( isset( $post->post_content ) &&
			( has_shortcode( $post->post_content, 'mbs_books' ) ||
			  has_shortcode( $post->post_content, 'mbs_book' ) ||
			  has_shortcode( $post->post_content, 'mbs_search' ) ||
			  has_shortcode( $post->post_content, 'mbs_author' ) ||
			  has_shortcode( $post->post_content, 'mbs_author_directory' ) ||
			  has_shortcode( $post->post_content, 'mbs_esp_signup' ) ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Output the CSS custom properties style block.
	 *
	 * Skips silently when: license not registered, or current page
	 * contains no MBS content.
	 *
	 * FREE tier receives 4 tokens; PRO tier receives all 20.
	 * Keys are stored in wp_options with underscores and output as
	 * CSS custom properties with hyphens (--mbs-{key-with-hyphens}).
	 *
	 * @return void
	 */
	public static function output(): void {
		if ( ! mbs_is_registered() ) {
			return;
		}

		if ( ! self::current_page_has_mbs_content() ) {
			return;
		}

		// V6-D-007: Skip if mbs-front CSS is already enqueued — that path handles token injection.
		if ( wp_style_is( 'mbs-front', 'done' ) || wp_style_is( 'mbs-front', 'enqueued' ) ) {
			return;
		}

		// FREE tokens (4)
		$colors = array(
			'accent'      => mbs_get_setting( 'appearance', 'accent',      '#4AABB8' ),
			'button-bg'   => mbs_get_setting( 'appearance', 'button_bg',   '#2C3E6B' ),
			'button-text' => mbs_get_setting( 'appearance', 'button_text', '#FFFFFF' ),
			'link'        => mbs_get_setting( 'appearance', 'link',        '#4AABB8' ),
		);

		if ( mbs_is_pro() ) {
			// PRO tokens (16) — stored with underscores, output with hyphens.
			$pro_colors = array(
				'navy'           => mbs_get_setting( 'appearance', 'navy',           '#2C3E6B' ),
				'navy-dark'      => mbs_get_setting( 'appearance', 'navy_dark',      '#1A2B50' ),
				'lavender'       => mbs_get_setting( 'appearance', 'lavender',       '#C8B4D4' ),
				'teal-pale'      => mbs_get_setting( 'appearance', 'teal_pale',      '#D4EEF1' ),
				'lavender-pale'  => mbs_get_setting( 'appearance', 'lavender_pale',  '#EDE8F4' ),
				'beige'          => mbs_get_setting( 'appearance', 'beige',          '#F4F0E8' ),
				'slate'          => mbs_get_setting( 'appearance', 'slate',          '#5A6472' ),
				'mist'           => mbs_get_setting( 'appearance', 'mist',           '#9AABBA' ),
				'coming-soon-bg'   => mbs_get_setting( 'appearance', 'coming_soon_bg',   '#F59E0B' ),
				'coming-soon-text' => mbs_get_setting( 'appearance', 'coming_soon_text', '#FFFFFF' ),
				'pro-bg'         => mbs_get_setting( 'appearance', 'pro_bg',         '#EDE8F4' ),
				'pro-text'       => mbs_get_setting( 'appearance', 'pro_text',       '#5A3A72' ),
				'card-bg'        => mbs_get_setting( 'appearance', 'card_bg',        '#FFFFFF' ),
				'border'         => mbs_get_setting( 'appearance', 'border',         '#E0D8C8' ),
				'watermark-text' => mbs_get_setting( 'appearance', 'watermark_text', '#9AABBA' ),
				'star'           => mbs_get_setting( 'appearance', 'star',           '#F5A623' ),
			);
			$colors = array_merge( $colors, $pro_colors );
		}

		echo '<style id="mbs-color-vars">';
		echo ':root .mbs-wrapper {';
		foreach ( $colors as $css_key => $value ) {
			$safe_value = MBS_Color_Helper::sanitize( $value );
			echo '--mbs-' . esc_attr( $css_key ) . ': ' . esc_attr( $safe_value ) . '; ';
		}
		echo '}';
		echo '</style>';
	}
}
