<?php
/**
 * Render template for Child Author Page Title block
 *
 * @package Author_Website_Templates
 */

// If direct access, exit
if (!defined('ABSPATH')) {
    exit;
}

// Attributes with defaults
$titleStyle = isset($attributes['titleStyle']) ? $attributes['titleStyle'] : 'fun';
$badgeText = isset($attributes['badgeText']) ? $attributes['badgeText'] : 'Biography';
$heading = isset($attributes['heading']) ? $attributes['heading'] : 'So Nice to Meet You!';
$subHeading = isset($attributes['subHeading']) ? $attributes['subHeading'] : 'I\'m Danielle Steel, a writer of silly stories and a lover of big adventures.';
$sectionBgColor = isset($attributes['sectionBgColor']) ? $attributes['sectionBgColor'] : '';
$curveColor = isset($attributes['curveColor']) ? $attributes['curveColor'] : '#FFFDF5';
$headingColor = isset($attributes['headingColor']) ? $attributes['headingColor'] : '';
$textColor = isset($attributes['textColor']) ? $attributes['textColor'] : '';
$blockId = isset($attributes['blockId']) ? $attributes['blockId'] : '';

// Render Dynamic Content based on context
if (is_archive()) {
    $heading = get_the_archive_title();
    $subHeading = get_the_archive_description();
} elseif (is_search()) {
    $heading = sprintf('Search Results for: %s', get_search_query());
    $subHeading = '';
} elseif (is_404()) {
    $heading = __('Page Not Found', 'author-website-templates');
    $subHeading = __('It looks like nothing was found at this location.', 'author-website-templates');
} elseif (is_home() && !is_front_page()) {
    $heading = single_post_title('', false);
    $subHeading = '';
}


// Set default background color based on style if not explicitly set
if (empty($sectionBgColor)) {
    if ($titleStyle === 'elegant') {
        $sectionBgColor = '#f8fafc';
    } elseif ($titleStyle === 'author-pro') {
        $sectionBgColor = ''; // Uses bg-paper class
    } else {
        $sectionBgColor = 'rgba(0, 198, 255, 0.1)';
    }
}

// Classes
$container_class = 'alignfull relative overflow-hidden pt-20 pb-28';
if ($titleStyle === 'author-pro') {
    $container_class = 'relative pt-20 pb-20 bg-paper border-b border-gray-100 overflow-hidden';
}

if (!empty($blockId)) {
    $container_class .= ' block-' . esc_attr($blockId);
}

// Heading Classes
if ($titleStyle === 'elegant') {
    $heading_class = 'text-5xl md:text-7xl font-serif font-bold mb-6';
    if (empty($headingColor)) {
        $heading_class .= ' text-brand-dark';
    }
} elseif ($titleStyle === 'author-pro') {
    $heading_class = 'text-4xl md:text-5xl lg:text-6xl font-serif font-bold mb-6 leading-tight';
    if (empty($headingColor)) {
        $heading_class .= ' text-primary';
    }
} else {
    // Fun (default)
    $heading_class = 'text-5xl md:text-7xl font-black mb-4';
    if (empty($headingColor)) {
        $heading_class .= ' text-navy';
    }
}

// Subheading Classes
if ($titleStyle === 'elegant') {
    $sub_heading_class = 'text-xl max-w-2xl mx-auto font-light leading-relaxed';
    if (empty($textColor)) {
        $sub_heading_class .= ' text-brand-gray';
    }
} elseif ($titleStyle === 'author-pro') {
    $sub_heading_class = 'text-lg leading-relaxed max-w-2xl mx-auto';
    if (empty($textColor)) {
        $sub_heading_class .= ' text-secondary';
    }
} else {
    // Fun (default)
    $sub_heading_class = 'text-xl max-w-lg mx-auto';
    if (empty($textColor)) {
        $sub_heading_class .= ' text-navy/80';
    }
}

$inner_container_class = 'awt-container mx-auto px-6 relative z-10 text-center';
if ($titleStyle === 'author-pro') {
    $inner_container_class .= ' max-w-4xl';
}
if (is_tax('book-author')) {
    return;
}
?>

<section class="<?php echo esc_attr($container_class); ?>"
    style="<?php echo !empty($sectionBgColor) ? 'background-color: ' . esc_attr($sectionBgColor) . ';' : ''; ?>">

    <!-- Bottom SVG Wave - Only for Fun Style -->
    <?php if ($titleStyle !== 'elegant' && $titleStyle !== 'author-pro'): ?>
        <div class="absolute bottom-0 left-0 w-full">
            <svg viewBox="0 0 1440 100" fill="none" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none"
                class="w-full h-auto">
                <path d="M0 100H1440V0C1192.5 49.497 919.5 74.497 720 74.497C520.5 74.497 247.5 49.497 0 0V100Z"
                    fill="<?php echo esc_attr($curveColor); ?>" />
            </svg>
        </div>
    <?php endif; ?>

    <div class="<?php echo esc_attr($inner_container_class); ?>">
        <!-- Badge - Only for Elegant Style -->
        <?php if ($titleStyle === 'elegant' && !empty($badgeText)): ?>
            <span class="font-bold text-gray-500 uppercase tracking-widest text-xs mb-4 block">
                <?php echo esc_html($badgeText); ?>
            </span>
        <?php endif; ?>

        <?php if (!empty($heading)): ?>
            <h1 class="<?php echo esc_attr($heading_class); ?>" <?php echo !empty($headingColor) ? 'style="color: ' . esc_attr($headingColor) . ';"' : ''; ?>>
                <?php echo wp_kses_post($heading); ?>
            </h1>
        <?php endif; ?>

        <?php if (!empty($subHeading)): ?>
            <p class="<?php echo esc_attr($sub_heading_class); ?>" <?php echo !empty($textColor) ? 'style="color: ' . esc_attr($textColor) . ';"' : ''; ?>>
                <?php echo wp_kses_post($subHeading); ?>
            </p>
        <?php endif; ?>
    </div>
</section>