<?php
/**
 * Server-side rendering for Child Author Header Block
 */

$menu_slug = isset($attributes['menuSlug']) ? $attributes['menuSlug'] : '';
$cta_text = isset($attributes['ctaText']) ? $attributes['ctaText'] : 'Latest Book!';
$cta_url = isset($attributes['ctaUrl']) ? $attributes['ctaUrl'] : '#featured-book';

// Colors
$header_bg_color = isset($attributes['headerBgColor']) && !empty($attributes['headerBgColor']) ? $attributes['headerBgColor'] : (isset($attributes['navBgColor']) ? $attributes['navBgColor'] : '');
$site_title_color = isset($attributes['siteTitleColor']) ? $attributes['siteTitleColor'] : '';
$link_color = isset($attributes['linkColor']) ? $attributes['linkColor'] : '';
$btn_bg_color = isset($attributes['btnBgColor']) ? $attributes['btnBgColor'] : '';
$btn_text_color = isset($attributes['btnTextColor']) ? $attributes['btnTextColor'] : '';
$btn_hover_bg_color = isset($attributes['btnHoverBgColor']) ? $attributes['btnHoverBgColor'] : '';
$btn_hover_text_color = isset($attributes['btnHoverTextColor']) ? $attributes['btnHoverTextColor'] : '';

$block_id = isset($attributes['blockId']) ? $attributes['blockId'] : 'header-' . uniqid();
$header_style = isset($attributes['headerStyle']) ? $attributes['headerStyle'] : 'fun';

// Logo
$logo_type = isset($attributes['logoType']) ? $attributes['logoType'] : 'custom';
$logo_text = isset($attributes['logoText']) ? $attributes['logoText'] : 'Danielle Steel';
$logo_image_url = isset($attributes['logoImageUrl']) ? $attributes['logoImageUrl'] : '';


if (function_exists('rswpthemes_awt_get_menu_tree')) {
    $menu_tree = rswpthemes_awt_get_menu_tree($menu_slug);
} else {
    $menu_tree = [];
}

$is_elegant = $header_style === 'elegant';

// --- LOGO LOGIC ---
$logo_content = '';
if ($logo_type === 'image' && !empty($logo_image_url)) {
    $logo_content = sprintf('<img src="%s" alt="%s" class="awt-logo-img w-32 md:w-40 h-auto" />', esc_url($logo_image_url), esc_attr(get_bloginfo('name')));
} elseif ($logo_type === 'site_title') {
    $logo_content = get_bloginfo('name');
} else {
    $logo_content = esc_html($logo_text);
}


// --- STYLING LOGIC ---

// Container Classes
$container_classes = $is_elegant 
    ? 'bg-white/95 backdrop-blur-sm sticky top-0 z-50 border-b border-gray-200 transition-all duration-300'
    : 'bg-white/80 backdrop-blur-sm sticky top-0 z-50 shadow-sm border-b border-yellow-sun/30 transition-all duration-300';

// Logo Classes
$logo_classes = $is_elegant
    ? 'awt-site-title text-2xl font-serif font-bold text-slate-800 tracking-tight hover:text-slate-900 transition-colors'
    : 'awt-site-title text-2xl md:text-3xl font-black text-navy tracking-tighter hover:text-blue-sky transition-colors';

// CTA Classes
$cta_classes = $is_elegant
    ? 'hidden md:inline-block px-6 py-2 bg-slate-900 text-white font-medium rounded text-sm hover:bg-gray-800 transition-all awt-cta-btn'
    : 'hidden md:inline-block btn-fun btn-yellow text-sm awt-cta-btn';

// Toggle Classes
$toggle_classes = $is_elegant
    ? 'lg:hidden mobile-toggle-btn text-slate-800 focus:outline-none'
    : 'lg:hidden mobile-toggle-btn text-navy hover:text-blue-sky focus:outline-none';


// Dynamic Styles (Scoped)
$styles = [];

if ($header_bg_color) {
    $styles[] = ".block-{$block_id} { background-color: {$header_bg_color} !important; }";
}
if ($site_title_color) {
    $styles[] = ".block-{$block_id} .awt-site-title { color: {$site_title_color} !important; }";
}
if ($link_color) {
    $styles[] = ".block-{$block_id} ul li a { color: {$link_color} !important; }";
    // Also target mobile
    $styles[] = ".block-{$block_id} .mobile-menu a { color: {$link_color} !important; }";
}
if ($btn_bg_color || $btn_text_color) {
    $bg = $btn_bg_color ? "background-color: {$btn_bg_color} !important; border-color: {$btn_bg_color} !important;" : '';
    $color = $btn_text_color ? "color: {$btn_text_color} !important;" : '';
    $styles[] = ".block-{$block_id} .awt-cta-btn { {$bg} {$color} }";
}
if ($btn_hover_bg_color || $btn_hover_text_color) {
    $bg = $btn_hover_bg_color ? "background-color: {$btn_hover_bg_color} !important; border-color: {$btn_hover_bg_color} !important;" : '';
    $color = $btn_hover_text_color ? "color: {$btn_hover_text_color} !important;" : '';
    $styles[] = ".block-{$block_id} .awt-cta-btn:hover { {$bg} {$color} }";
}

$scoped_style_block = !empty($styles) ? "<style>" . implode(' ', $styles) . "</style>" : "";

$show_cta = !empty($cta_text) && !empty($cta_url);

// --- MENU RENDERING ---

// Desktop Menu
$desktop_menu_html = '';
if (function_exists('rswpthemes_awt_render_desktop_menu')) {
    $desktop_menu_html = rswpthemes_awt_render_desktop_menu($menu_tree, 1, $link_color);
    
    if ($is_elegant) {
        // Swap UL classes
        $fun_ul = 'hidden md:flex space-x-6 lg:space-x-8 items-center font-bold text-navy/80';
        $ele_ul = 'hidden lg:flex space-x-8 items-center font-medium text-sm text-slate-800/80 uppercase tracking-wide';
        $desktop_menu_html = str_replace($fun_ul, $ele_ul, $desktop_menu_html);

        // Swap Link classes
        $fun_link = 'hover:text-blue-sky';
        $ele_link = 'hover:text-slate-900 transition-colors';
        $desktop_menu_html = str_replace($fun_link, $ele_link, $desktop_menu_html);
    }
}

// Mobile Menu Default Color
$default_link_color = $is_elegant ? '#1e293b' : '#264653';
$mobile_link_color = !empty($link_color) ? $link_color : $default_link_color;
?>

<?php echo $scoped_style_block; ?>

<header
    class="awt-header-block <?php echo esc_attr($container_classes); ?> block-<?php echo esc_attr($block_id); ?>">
    <!-- Note: nav tag gets background by default in classes, but scoped style targets it via ID if overridden -->
    <nav class="awt-container mx-auto px-6 py-4 flex justify-between items-center relative">

        <a href="<?php echo esc_url(home_url('/')); ?>"
            class="<?php echo esc_attr($logo_classes); ?>">
            <?php echo $logo_content; ?>
        </a>

        <?php echo $desktop_menu_html; ?>

        <div class="flex items-center gap-4">
            <?php if ($show_cta): ?>
                <a href="<?php echo esc_url($cta_url); ?>" class="<?php echo esc_attr($cta_classes); ?>">
                    <?php echo esc_html($cta_text); ?>
                </a>
            <?php endif; ?>

            <button id="mobile-menu-btn"
                class="<?php echo esc_attr($toggle_classes); ?>">
                <svg class="w-8 h-8" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 12h16m-7 6h7"></path>
                </svg>
            </button>
        </div>
    </nav>

    <div
        class="mobile-menu hidden lg:hidden bg-white border-t border-gray-100 absolute left-0 top-full w-full shadow-lg z-40">
        <?php
        if (function_exists('rswpthemes_awt_render_mobile_menu')) {
            echo rswpthemes_awt_render_mobile_menu($menu_tree, $mobile_link_color);
        }
        ?>

        <?php if ($show_cta): ?>
            <div class="p-4 border-t border-gray-100">
                <a href="<?php echo esc_url($cta_url); ?>"
                    class="block text-center px-6 py-2.5 bg-yellow-sun text-navy font-black rounded-full shadow-md hover:bg-yellow-400 transition-all text-sm awt-cta-btn">
                    <?php echo esc_html($cta_text); ?>
                </a>
            </div>
        <?php endif; ?>
    </div>
</header>