<?php
/**
 * The WooCommerce template file
 * 
 * @package Paws_And_Care
 * @since 1.0.0
 */

get_header();

// Sidebar layout — disable sidebar on single product, cart, checkout, account pages, or if widget area is empty
$is_full_width = is_product() || is_cart() || is_checkout() || is_account_page() || ! is_active_sidebar( 'sidebar-woocommerce' );
$sidebar_layout = paws_and_care_get_sidebar_layout();

if ( $is_full_width ) {
    $sidebar_layout['show_sidebar']  = false;
    $sidebar_layout['content_class'] = 'col-12';
}

// Page Header Logic
$title = '';
$subtitle = '';
$breadcrumbs = array();

// Start with Home
$breadcrumbs[] = array(
    'label' => __( 'Home', 'paws-and-care' ),
    'url'   => home_url( '/' ),
);

if ( is_shop() ) {
    $shop_page_id = wc_get_page_id( 'shop' );
    $title = get_the_title( $shop_page_id );
    $subtitle = '';
    $breadcrumbs[] = array(
        'label' => $title,
        'url'   => '',
    );
} elseif ( is_product() ) {
    $title = get_the_title();
    $subtitle = '';
    
    // Add Shop link
    $shop_page_id = wc_get_page_id( 'shop' );
    if ( $shop_page_id ) {
        $breadcrumbs[] = array(
            'label' => get_the_title( $shop_page_id ),
            'url'   => get_permalink( $shop_page_id ),
        );
    }
    
    // Add categories
    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );
    if ( $terms && ! is_wp_error( $terms ) ) {
        // Find the most specific category (first one for simplicity)
        $term = $terms[0];
        
        // Get category parents
        $ancestors = get_ancestors( $term->term_id, 'product_cat' );
        $ancestors = array_reverse( $ancestors );
        
        foreach ( $ancestors as $ancestor_id ) {
            $ancestor = get_term( $ancestor_id, 'product_cat' );
            $breadcrumbs[] = array(
                'label' => $ancestor->name,
                'url'   => get_term_link( $ancestor ),
            );
        }
        
        // Add the current category
        $breadcrumbs[] = array(
            'label' => $term->name,
            'url'   => get_term_link( $term ),
        );
    }
    
    // Add current product name
    $breadcrumbs[] = array(
        'label' => $title,
        'url'   => '',
    );
} elseif ( is_cart() ) {
    $title = __( 'Shopping Cart', 'paws-and-care' );
    $subtitle = __( 'Review your items before checkout.', 'paws-and-care' );
    $breadcrumbs[] = array(
        'label' => $title,
        'url'   => '',
    );
} elseif ( is_checkout() ) {
    $title = __( 'Checkout', 'paws-and-care' );
    $subtitle = __( 'Complete your order.', 'paws-and-care' );
    $breadcrumbs[] = array(
        'label' => $title,
        'url'   => '',
    );
} else {
    $title = woocommerce_page_title( false );
    $subtitle = '';
    
    // Add Shop link for categories/tags
    $shop_page_id = wc_get_page_id( 'shop' );
    if ( $shop_page_id && ( is_product_category() || is_product_tag() ) ) {
        $breadcrumbs[] = array(
            'label' => get_the_title( $shop_page_id ),
            'url'   => get_permalink( $shop_page_id ),
        );
    }
    
    $breadcrumbs[] = array(
        'label' => $title,
        'url'   => '',
    );
}

// Display the Page Header
get_template_part( 'template-parts/page-header', null, array(
    'title'       => $title,
    'subtitle'    => $subtitle,
    'breadcrumbs' => $breadcrumbs,
) );
?>

<div id="primary-content" class="woocommerce-page py-5">
    <div class="container">
        <div class="row">
            <div class="<?php echo esc_attr( trim( $sidebar_layout['content_class'] . ' ' . $sidebar_layout['content_order'] ) ); ?>">
                <div class="woocommerce">
                    <?php woocommerce_content(); ?>
                </div>
            </div>

            <?php if ( $sidebar_layout['show_sidebar'] ) : ?>
                <div class="<?php echo esc_attr( trim( $sidebar_layout['sidebar_class'] . ' ' . $sidebar_layout['sidebar_order'] ) ); ?>">
                    <?php get_sidebar( 'woocommerce' ); ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php
get_footer();

