<?php
/**
 * Template part for displaying the page header
 *
 * Uses customizer settings from Theme Settings > Page Header / Breadcrumb Settings
 *
 * @package Paws_And_Care
 * @since 0.1.2
 *
 * Available arguments (passed via $args):
 * - title: (string) Page title (optional, defaults to current page/post title)
 * - subtitle: (string) Page subtitle/lead text (optional)
 * - breadcrumbs: (array) Custom breadcrumb items (optional)
 *   Format: array( array( 'label' => 'Home', 'url' => '/' ), array( 'label' => 'About' ) )
 */

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

// Get customizer settings
$show_header = get_theme_mod('paws_and_care_show_page_header', true);
$header_bg_image = get_theme_mod('paws_and_care_page_header_bg', '');
$header_bg_color = get_theme_mod('paws_and_care_page_header_bg_color', '#f8f9fa');
$header_overlay = get_theme_mod('paws_and_care_page_header_overlay', 'rgba(0, 0, 0, 0.5)');
$show_breadcrumbs = get_theme_mod('paws_and_care_show_breadcrumbs', true);
$breadcrumb_style = get_theme_mod('paws_and_care_breadcrumb_style', 'pill');

// Don't show if disabled
if (!$show_header) {
    return;
}

// Get arguments with defaults (auto-detect if not passed)
if (isset($args['title'])) {
    $title = $args['title'];
} elseif (is_archive()) {
    // Strip HTML tags from archive title (WordPress wraps it in span tags)
    $title = wp_strip_all_tags(get_the_archive_title());
} else {
    $title = get_the_title();
}

// Auto-detect subtitle from excerpt/description if not passed
if (isset($args['subtitle'])) {
    $subtitle = $args['subtitle'];
} elseif (is_archive() && get_the_archive_description()) {
    $subtitle = wp_strip_all_tags(get_the_archive_description());
} elseif (has_excerpt()) {
    $subtitle = get_the_excerpt();
} else {
    $subtitle = '';
}

// Auto-generate breadcrumbs if not passed
if (isset($args['breadcrumbs'])) {
    $breadcrumbs = $args['breadcrumbs'];
} else {
    $breadcrumbs = array(
        array(
            'label' => __('Home', 'paws-and-care'),
            'url' => home_url('/'),
        ),
    );
    
    // Add Blog link for archive pages
    if (is_archive() || is_single()) {
        $blog_page_id = get_option('page_for_posts');
        if ($blog_page_id) {
            $breadcrumbs[] = array(
                'label' => __('Blog', 'paws-and-care'),
                'url' => get_permalink($blog_page_id),
            );
        }
    }
    
    // Add current page/archive title
    $breadcrumbs[] = array(
        'label' => $title,
        'url' => '',
    );
}

// Build inline styles
$section_styles = '';
$overlay_html = '';

if (!empty($header_bg_image)) {
    $section_styles = sprintf(
        'background-image: url(%s); background-size: cover; background-position: center; position: relative;',
        esc_url($header_bg_image)
    );
    $overlay_html = sprintf(
        '<div class="page-header-overlay" style="position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: %s; z-index: 1;"></div>',
        esc_attr($header_overlay)
    );
} elseif (!empty($header_bg_color) && '#f8f9fa' !== $header_bg_color) {
    $section_styles = sprintf('background-color: %s;', esc_attr($header_bg_color));
}

// Determine text color class based on background
$text_color_class = !empty($header_bg_image) ? 'text-white' : '';
$content_z_index = !empty($header_bg_image) ? 'position: relative; z-index: 2;' : '';
?>

<!-- Page Header -->
<section class="py-5 page-header-section" <?php echo $section_styles ? 'style="' . esc_attr($section_styles) . '"' : ''; ?>>
    <?php
    // Output overlay with proper escaping - only allow div with safe style attribute
    $allowed_html = array(
        'div' => array(
            'class' => array(),
            'style' => array(),
        ),
    );
    echo wp_kses($overlay_html, $allowed_html);
    ?>
    <div class="container" <?php echo $content_z_index ? 'style="' . esc_attr($content_z_index) . '"' : ''; ?>>
        <div class="row">
            <div class="col-12 text-center <?php echo esc_attr($text_color_class); ?>">
                <h1 class="display-4 fw-bold"><?php echo esc_html($title); ?></h1>
                <?php if (!empty($subtitle)): ?>
                    <p class="lead"><?php echo esc_html($subtitle); ?></p>
                <?php endif; ?>

                <?php if ($show_breadcrumbs && !empty($breadcrumbs)): ?>
                    <nav aria-label="breadcrumb"
                        class="breadcrumb-nav breadcrumb-style-<?php echo esc_attr($breadcrumb_style); ?>">
                        <?php if ('pill' === $breadcrumb_style): ?>
                            <div class="pill-breadcrumb">
                                <?php
                                $total = count($breadcrumbs);
                                foreach ($breadcrumbs as $index => $crumb):
                                    $is_last = ($index === $total - 1);
                                    if ($is_last || empty($crumb['url'])):
                                        ?>
                                        <span class="pill-btn active"><?php echo esc_html($crumb['label']); ?></span>
                                    <?php else: ?>
                                        <a href="<?php echo esc_url($crumb['url']); ?>"
                                            class="pill-btn"><?php echo esc_html($crumb['label']); ?></a>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </div>
                        <?php elseif ('arrow' === $breadcrumb_style): ?>
                            <div class="arrow-breadcrumb">
                                <?php
                                $total = count($breadcrumbs);
                                foreach ($breadcrumbs as $index => $crumb):
                                    $is_last = ($index === $total - 1);
                                    $is_first = ($index === 0);
                                    if (!$is_first):
                                        ?>
                                        <span class="breadcrumb-separator">→</span>
                                    <?php endif; ?>
                                    <?php if ($is_last || empty($crumb['url'])): ?>
                                        <span class="breadcrumb-current"><?php echo esc_html($crumb['label']); ?></span>
                                    <?php else: ?>
                                        <a href="<?php echo esc_url($crumb['url']); ?>"
                                            class="breadcrumb-link"><?php echo esc_html($crumb['label']); ?></a>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </div>
                        <?php else: ?>
                            <div class="standard-breadcrumb">
                                <?php
                                $total = count($breadcrumbs);
                                foreach ($breadcrumbs as $index => $crumb):
                                    $is_last = ($index === $total - 1);
                                    $is_first = ($index === 0);
                                    if (!$is_first):
                                        ?>
                                        <span class="breadcrumb-separator">/</span>
                                    <?php endif; ?>
                                    <?php if ($is_last || empty($crumb['url'])): ?>
                                        <span class="breadcrumb-current"><?php echo esc_html($crumb['label']); ?></span>
                                    <?php else: ?>
                                        <a href="<?php echo esc_url($crumb['url']); ?>"
                                            class="breadcrumb-link"><?php echo esc_html($crumb['label']); ?></a>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </nav>
                <?php endif; ?>
            </div>
        </div>
    </div>
</section>