<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}
/**
 * Template Part: Page Header
 *
 * Reusable page header component that matches the theme design
 *
 * @package Pawfect
 */

// Don't show if disabled
if (!get_theme_mod('pawfect_page_header_enable_disable', true)) {
    return;
}

// Get page header settings
$header_bg_image = get_theme_mod('pawfect_page_header_background_image', '');
$header_bg_color = get_theme_mod('pawfect_page_header_background_color', '#FFFFFF');
$header_text_color = get_theme_mod('pawfect_page_header_text_color', '#333333');
$header_height = get_theme_mod('pawfect_page_header_height', array('slider' => 300, 'suffix' => 'px'));
$overlay_opacity = get_theme_mod('pawfect_page_header_overlay_opacity', array('slider' => 50, 'suffix' => '%'));
$show_breadcrumbs = get_theme_mod('pawfect_page_header_breadcrumbs_enable', true);

// Extract height value
$height_value = is_array($header_height) ? $header_height['slider'] : 300;
$height_unit = is_array($header_height) ? $header_height['suffix'] : 'px';

// Extract overlay opacity value
$opacity_value = is_array($overlay_opacity) ? $overlay_opacity['slider'] : 50;

// Prepare inline styles
$header_styles = array();

// Add height
$header_styles[] = "min-height: " . intval($height_value) . $height_unit;

// Add background color
if (!empty($header_bg_color)) {
    $header_styles[] = "background-color: " . esc_attr($header_bg_color);
}

// Add background image if available
if (!empty($header_bg_image)) {
    $header_styles[] = "background-image: url('" . esc_url($header_bg_image) . "')";
    $header_styles[] = "background-size: cover";
    $header_styles[] = "background-position: center";
    $header_styles[] = "background-repeat: no-repeat";
    $header_styles[] = "background-attachment: fixed";
}

// Add text color
if (!empty($header_text_color)) {
    $header_styles[] = "color: " . esc_attr($header_text_color);
}

$style_attr = !empty($header_styles) ? 'style="' . implode('; ', $header_styles) . '"' : '';

// Get page data
$page_title = '';
$page_description = '';

// Determine page type and set appropriate variables
$is_woocommerce_page = false;
$woocommerce_page_type = '';
$breadcrumb_items = array();

// Check if WooCommerce is active
if (function_exists('is_woocommerce')) {
    $is_woocommerce_page = is_woocommerce() || is_cart() || is_checkout() || is_account_page();
}

// Determine title, description, and breadcrumbs based on context
if ($is_woocommerce_page) {
    // === WOOCOMMERCE PAGES ===

    if (is_product()) {
        // Single Product Page
        $woocommerce_page_type = 'product';
        $page_title = get_the_title();
        $page_description = get_the_excerpt();

        // Product breadcrumbs: Home > Shop > [Category] > Product
        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => __('Shop', 'pawfect'), 'url' => wc_get_page_permalink('shop'));

        // Add product categories
        $product_cats = get_the_terms(get_the_ID(), 'product_cat');
        if ($product_cats && !is_wp_error($product_cats)) {
            $main_cat = array_shift($product_cats);
            $breadcrumb_items[] = array('title' => $main_cat->name, 'url' => get_term_link($main_cat));
        }

        $breadcrumb_items[] = array('title' => get_the_title(), 'url' => '');

    }
    elseif (is_shop()) {
        // Shop Page
        $woocommerce_page_type = 'shop';
        $page_title = woocommerce_page_title(false);
        $page_description = '';

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_product_category()) {
        // Product Category Page
        $woocommerce_page_type = 'product_category';
        $page_title = single_term_title('', false);
        $page_description = term_description();

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => __('Shop', 'pawfect'), 'url' => wc_get_page_permalink('shop'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_product_tag()) {
        // Product Tag Page
        $woocommerce_page_type = 'product_tag';
        $page_title = single_term_title('', false);
        $page_description = term_description();

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => __('Shop', 'pawfect'), 'url' => wc_get_page_permalink('shop'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_cart()) {
        // Cart Page
        $woocommerce_page_type = 'cart';
        $page_title = get_the_title(wc_get_page_id('cart'));
        $page_description = '';

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => __('Shop', 'pawfect'), 'url' => wc_get_page_permalink('shop'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_checkout()) {
        // Checkout Page
        $woocommerce_page_type = 'checkout';
        $page_title = get_the_title(wc_get_page_id('checkout'));
        $page_description = '';

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => __('Shop', 'pawfect'), 'url' => wc_get_page_permalink('shop'));
        $breadcrumb_items[] = array('title' => get_the_title(wc_get_page_id('cart')), 'url' => wc_get_cart_url());
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_account_page()) {
        // My Account Page
        $woocommerce_page_type = 'account';
        $page_title = get_the_title(wc_get_page_id('myaccount'));
        $page_description = '';

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    else {
        // Other WooCommerce pages
        $woocommerce_page_type = 'other';
        $page_title = get_the_title();
        $page_description = get_the_excerpt();

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');
    }

}
else {
    // === NON-WOOCOMMERCE PAGES ===

    if (is_page()) {
        $page_title = get_the_title();
        $page_description = get_the_excerpt();

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_single()) {
        // Blog Post
        $page_title = get_the_title();
        $page_description = get_the_excerpt();

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => __('Blog', 'pawfect'), 'url' => get_permalink(get_option('page_for_posts')));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_category()) {
        $page_title = single_cat_title('', false);
        $page_description = category_description();

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => __('Blog', 'pawfect'), 'url' => get_permalink(get_option('page_for_posts')));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_tag()) {
        $page_title = single_tag_title('', false);
        $page_description = tag_description();

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => __('Blog', 'pawfect'), 'url' => get_permalink(get_option('page_for_posts')));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_author()) {
        $page_title = get_the_author();
        $page_description = get_the_author_meta('description');

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => __('Blog', 'pawfect'), 'url' => get_permalink(get_option('page_for_posts')));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_archive()) {
        $page_title = get_the_archive_title();
        $page_description = get_the_archive_description();

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_search()) {
        $page_title = sprintf(__('Search Results for: %s', 'pawfect'), get_search_query());
        $page_description = '';

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_home()) {
        // Blog index page (when "Your latest posts" is selected in Reading Settings)
        // Use the same title as the blog section from customizer
        $page_title = get_theme_mod('pawfect_blog_area_title', __('Latest News', 'pawfect'));
        $page_description = get_theme_mod('pawfect_blog_area_des', __('Tips, stories & ideas for every dog lover', 'pawfect'));

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    elseif (is_404()) {
        $page_title = __('Page Not Found', 'pawfect');
        $page_description = __('The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.', 'pawfect');

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');

    }
    else {
        $page_title = get_the_title();
        $page_description = get_the_excerpt();

        $breadcrumb_items[] = array('title' => __('Home', 'pawfect'), 'url' => home_url('/'));
        $breadcrumb_items[] = array('title' => $page_title, 'url' => '');
    }
}

// Clean up description
$page_description = wp_strip_all_tags($page_description);
?>

<!-- Enhanced Page Header Section -->
<section class="pawfect-page-header pawfect-relative pawfect-py-20 lg:pawfect-py-28" <?php echo wp_kses_post($style_attr); ?>>

    <div class="pawfect-page-header-heading pawfect-container pawfect-relative pawfect-text-center">
        
        <?php if ($show_breadcrumbs && (!is_front_page() || is_home()) && !empty($breadcrumb_items)): ?>
            <!-- Enhanced Breadcrumbs -->
            <nav class="pawfect-breadcrumbs pawfect-mb-8" aria-label="<?php esc_attr_e( 'Breadcrumb', 'pawfect' ); ?>">
                <ol class="pawfect-flex pawfect-items-center pawfect-justify-center pawfect-gap-2 pawfect-text-sm pawfect-opacity-80">
                    <?php
    $total_items = count($breadcrumb_items);
    foreach ($breadcrumb_items as $index => $item):
        $is_last = ($index === $total_items - 1);
?>
                        <li>
                            <?php if (!$is_last && !empty($item['url'])): ?>
                                <a href="<?php echo esc_url($item['url']); ?>" class="hover:pawfect-text-pawfect-primary pawfect-transition-colors">
                                    <?php echo esc_html($item['title']); ?>
                                </a>
                            <?php
        else: ?>
                                <span class="pawfect-font-medium">
                                    <?php echo esc_html($item['title']); ?>
                                </span>
                            <?php
        endif; ?>
                        </li>

                        <?php if (!$is_last): ?>
                            <li class="pawfect-opacity-50">
                                <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
                                    <path d="M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6-1.41-1.41z"/>
                                </svg>
                            </li>
                        <?php
        endif; ?>
                    <?php
    endforeach; ?>
                </ol>
            </nav>
        <?php
endif; ?>

        <!-- Page Title -->
        <h1 class="pawfect-page-title-heading pawfect-text-4xl md:pawfect-text-5xl lg:pawfect-text-6xl pawfect-font-bold pawfect-mb-6">
            <?php echo esc_html($page_title); ?>
        </h1>

        <!-- WooCommerce Page Meta -->
        <?php if ($is_woocommerce_page): ?>
            <div class="pawfect-flex pawfect-flex-wrap pawfect-items-center pawfect-justify-center pawfect-gap-6 pawfect-opacity-90 pawfect-mb-6">

                <?php if ($woocommerce_page_type === 'product'): ?>
                    <!-- Product Price -->
                    <?php
        global $product;
        if ($product && $product->get_price()): ?>
                        <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1"></path>
                            </svg>
                            <span class="pawfect-font-medium pawfect-text-accent">
                                <?php echo wp_kses_post($product->get_price_html()); ?>
                            </span>
                        </div>
                    <?php
        endif; ?>

                    <!-- Product Categories -->
                    <?php
        $product_cats = get_the_terms(get_the_ID(), 'product_cat');
        if ($product_cats && !is_wp_error($product_cats)): ?>
                        <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"></path>
                            </svg>
                            <span class="pawfect-font-medium">
                                <?php
            $cat_names = array();
            foreach ($product_cats as $cat) {
                $cat_names[] = $cat->name;
            }
            echo esc_html(implode(', ', $cat_names));
?>
                            </span>
                        </div>
                    <?php
        endif; ?>

                    <!-- Product SKU -->
                    <?php if ($product && $product->get_sku()): ?>
                        <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
                            </svg>
                            <span class="pawfect-font-medium">
                                <?php esc_html_e('SKU:', 'pawfect'); ?> <?php echo esc_html($product->get_sku()); ?>
                            </span>
                        </div>
                    <?php
        endif; ?>

                <?php
    elseif ($woocommerce_page_type === 'shop'): ?>
                    <!-- Shop Page Info -->
                    <?php
        $product_count = wp_count_posts('product');
        if ($product_count && $product_count->publish > 0): ?>
                        <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 11V7a4 4 0 00-8 0v4M5 9h14l1 12H4L5 9z"></path>
                            </svg>
                            <span class="pawfect-font-medium">
                                <?php echo esc_html(number_format($product_count->publish)); ?> <?php echo esc_html(_n('Product', 'Products', $product_count->publish, 'pawfect')); ?>
                            </span>
                        </div>
                    <?php
        endif; ?>

                <?php
    elseif ($woocommerce_page_type === 'product_category' || $woocommerce_page_type === 'product_tag'): ?>
                    <!-- Category/Tag Info - Show product count if available -->
                    <?php
        $term = get_queried_object();
        if ($term && isset($term->count) && $term->count > 0): ?>
                        <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"></path>
                            </svg>
                            <span class="pawfect-font-medium">
                                <?php echo esc_html(number_format($term->count)); ?> <?php echo esc_html(_n('Product', 'Products', $term->count, 'pawfect')); ?>
                            </span>
                        </div>
                    <?php
        endif; ?>

                <?php
    elseif ($woocommerce_page_type === 'cart'): ?>
                    <!-- Cart Info -->
                    <?php
        $cart_count = WC()->cart ? WC()->cart->get_cart_contents_count() : 0;
        if ($cart_count > 0): ?>
                        <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4m0 0L7 13m0 0l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17M17 13v4a2 2 0 01-2 2H9a2 2 0 01-2-2v-4m8 0V9a2 2 0 00-2-2H9a2 2 0 00-2 2v4.01"></path>
                            </svg>
                            <span class="pawfect-font-medium">
                                <?php echo esc_html(number_format($cart_count)); ?> <?php echo esc_html(_n('Item', 'Items', $cart_count, 'pawfect')); ?>
                            </span>
                        </div>
                    <?php
        endif; ?>

                <?php
    elseif ($woocommerce_page_type === 'checkout'): ?>
                    <!-- Checkout Info - Show cart total if available -->
                    <?php
        $cart_total = WC()->cart ? WC()->cart->get_cart_total() : '';
        if (!empty($cart_total)): ?>
                        <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                            </svg>
                            <span class="pawfect-font-medium">
                                <?php echo wp_kses_post($cart_total); ?>
                            </span>
                        </div>
                    <?php
        endif; ?>

                <?php
    elseif ($woocommerce_page_type === 'account'): ?>
                    <!-- Account Info - Show user info if logged in -->
                    <?php if (is_user_logged_in()): ?>
                        <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                            <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
                            </svg>
                            <span class="pawfect-font-medium">
                                <?php printf(esc_html__('Welcome, %s', 'pawfect'), esc_html(wp_get_current_user()->display_name)); ?>
                            </span>
                        </div>
                    <?php
        endif; ?>
                <?php
    endif; ?>

            </div>
        <?php
endif; ?>

        <!-- Post Meta for Blog Posts Only -->
        <?php if (!$is_woocommerce_page && is_single()): ?>
            <div class="pawfect-flex pawfect-flex-wrap pawfect-items-center pawfect-justify-center pawfect-gap-6 pawfect-opacity-90 pawfect-mb-6">
                <!-- Post Date -->
                <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                    <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"></path>
                    </svg>
                    <span class="pawfect-font-medium"><?php echo esc_html(get_the_date()); ?></span>
                </div>

                <!-- Post Author -->
                <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                    <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"></path>
                    </svg>
                    <span class="pawfect-font-medium"><?php echo esc_html(get_the_author()); ?></span>
                </div>

                <!-- Post Categories -->
                <?php
    $categories = get_the_category();
    if (!empty($categories)): ?>
                    <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                        <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 7h.01M7 3h5c.512 0 1.024.195 1.414.586l7 7a2 2 0 010 2.828l-7 7a2 2 0 01-2.828 0l-7-7A1.994 1.994 0 013 12V7a4 4 0 014-4z"></path>
                        </svg>
                        <span class="pawfect-font-medium">
                            <?php
        $category_links = array();
        foreach ($categories as $category) {
            $category_links[] = '<a href="' . esc_url(get_category_link($category->term_id)) . '">' . esc_html($category->name) . '</a>';
        }
        echo wp_kses_post(implode(', ', $category_links));
?>
                        </span>
                    </div>
                <?php
    endif; ?>

                <!-- Reading Time Estimate -->
                <?php
    $content = get_post_field('post_content', get_the_ID());
    $word_count = str_word_count(strip_tags($content));
    $reading_time = ceil($word_count / 200); // Average reading speed
?>
                <div class="pawfect-flex pawfect-items-center pawfect-gap-2">
                    <svg class="pawfect-w-5 pawfect-h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                    </svg>
                    <span class="pawfect-font-medium"><?php echo absint($reading_time); ?> <?php esc_html_e('min read', 'pawfect'); ?></span>
                </div>
            </div>
        <?php
endif; ?>

      


       

    </div>


</section>
