<?php
declare(strict_types=1);
/**
 * Customizer — Dynamic CSS Output.
 *
 * Generates inline CSS that overrides CSS Custom Properties.
 * Uses wp_head to ensure output AFTER all stylesheets are loaded,
 * preventing @import race conditions.
 *
 * @package WTE_Business_Card
 * @since   1.0.0
 */
if ( ! defined( 'ABSPATH' ) ) { exit; }

function wte_customizer_dynamic_css(): void {
	$css = '';

	// Colors.
	$color_map = [
		'wte_color_header_bg'      => [ '--wte-color-header-bg',      '#ffffff' ],
		'wte_color_header_text'    => [ '--wte-color-header-text',    '#1a1a1a' ],
		'wte_color_footer_bg'      => [ '--wte-color-footer-bg',      '#3c4043' ],
		'wte_color_footer_text'    => [ '--wte-color-footer-text',    '#f8f9fa' ],
		'wte_color_accent'         => [ '--wte-color-accent',         '#2d6be4' ],
		'wte_color_link'           => [ '--wte-color-link',           '#2d6be4' ],
		'wte_color_body_bg'        => [ '--wte-color-body-bg',        '#ffffff' ],
		'wte_color_section_alt_bg' => [ '--wte-color-section-alt-bg', '#f8f9fa' ],
	];

	foreach ( $color_map as $mod_id => $config ) {
		$val = get_theme_mod( $mod_id, $config[1] );
		$safe = sanitize_hex_color( $val );
		if ( $safe ) {
			$css .= $config[0] . ':' . $safe . ';';
		}
	}

	// Fonts.
	$body_font    = get_theme_mod( 'wte_font_body', 'noto_sans_jp' );
	$heading_font = get_theme_mod( 'wte_font_heading', 'noto_serif_jp' );
	$css .= '--wte-font-body:' . wte_get_font_family( $body_font ) . ';';
	$css .= '--wte-font-heading:' . wte_get_font_family( $heading_font ) . ';';

	// Font size.
	$font_size = get_theme_mod( 'wte_font_size_base', 16 );
	$css .= '--wte-font-size-base:' . absint( $font_size ) . 'px;';

	// Header.
	$header_height = get_theme_mod( 'wte_header_height', 72 );
	$css .= '--wte-header-height:' . absint( $header_height ) . 'px;';

	$logo_width = get_theme_mod( 'wte_site_logo_width', 200 );
	$css .= '--wte-logo-width:' . absint( $logo_width ) . 'px;';

	// Hero overlay.
	$overlay_opacity = get_theme_mod( 'wte_hero_overlay_opacity', 40 );
	$css .= '--hero-overlay-opacity:' . ( absint( $overlay_opacity ) / 100 ) . ';';

	$overlay_color = get_theme_mod( 'wte_hero_overlay_color', '#000000' );
	$safe_overlay  = sanitize_hex_color( $overlay_color );
	if ( $safe_overlay ) {
		$css .= '--hero-overlay-color:' . $safe_overlay . ';';
	}

	// Link hover (darken accent).
	$accent = get_theme_mod( 'wte_color_accent', '#2d6be4' );
	$css .= '--wte-color-link-hover:' . wte_darken_color( $accent, 20 ) . ';';

	// Output as <style> in wp_head — runs AFTER all enqueued
	// stylesheets, so it always wins over @import defaults.
	if ( $css ) {
		echo '<style id="wte-customizer-css">:root{' . $css . '}</style>' . PHP_EOL;
	}
}
add_action( 'wp_head', 'wte_customizer_dynamic_css', 99 );

/**
 * Darken a hex color by a percentage.
 */
function wte_darken_color( string $hex, int $percent ): string {
	$hex = ltrim( $hex, '#' );
	if ( strlen( $hex ) === 3 ) {
		$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
	}
	if ( strlen( $hex ) !== 6 ) {
		return '#000000';
	}
	$r = max( 0, (int) hexdec( substr( $hex, 0, 2 ) ) - (int) round( 255 * $percent / 100 ) );
	$g = max( 0, (int) hexdec( substr( $hex, 2, 2 ) ) - (int) round( 255 * $percent / 100 ) );
	$b = max( 0, (int) hexdec( substr( $hex, 4, 2 ) ) - (int) round( 255 * $percent / 100 ) );
	return '#' . str_pad( dechex( $r ), 2, '0', STR_PAD_LEFT )
	           . str_pad( dechex( $g ), 2, '0', STR_PAD_LEFT )
	           . str_pad( dechex( $b ), 2, '0', STR_PAD_LEFT );
}
