<?php
/**
 * Howcan: Bootstrap light breadcrumb for single post
 * Place this in single.php or template-part and call where you want the breadcrumb to appear.
 * Requires Bootstrap CSS (breadcrumb, bg-light, rounded, py classes).
 */

if ( ! is_single() ) {
    return;
}

global $post;

// Helper: get primary category (Yoast fallback to first category)
function howcan_get_primary_category( $post_id ) {
    // try Yoast primary category
    if ( class_exists( 'WPSEO_Primary_Term' ) ) {
        $wpseo_primary_term = new WPSEO_Primary_Term( 'category', $post_id );
        $primary_term_id = $wpseo_primary_term->get_primary_term();
        if ( $primary_term_id && ! is_wp_error( $primary_term_id ) ) {
            $term = get_category( (int) $primary_term_id );
            if ( $term ) return $term;
        }
    }

    // fallback: first category assigned
    $cats = get_the_category( $post_id );
    if ( ! empty( $cats ) ) {
        return $cats[0];
    }

    return false;
}

// Build category trail (handles parent categories)
function howcan_get_category_parents( $cat ) {
    $trail = array();
    while ( $cat && ! is_wp_error( $cat ) ) {
        $trail[] = $cat;
        if ( $cat->parent ) {
            $cat = get_category( $cat->parent );
        } else {
            break;
        }
    }
    // trail currently from child -> ancestor, reverse to ancestor -> child
    return array_reverse( $trail );
}

$primary_cat = howcan_get_primary_category( $post->ID );
?>

<nav aria-label="<?php esc_attr_e( 'breadcrumb', 'howcan' ); ?>" class="my-2">
  <ol style="width:100%;" class="breadcrumb rounded m-0">
    <!-- Home -->
    <li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
      <a href="<?php echo esc_url( home_url( '/' ) ); ?>" itemprop="item">
        <span itemprop="name"><?php esc_html_e( 'Home', 'howcan' ); ?></span>
      </a>
      <meta itemprop="position" content="1" />
    </li>

    <?php if ( $primary_cat ) :
      $parents = howcan_get_category_parents( $primary_cat );
      $position = 2; // next position for schema
      foreach ( $parents as $cat ) : ?>
        <li class="breadcrumb-item" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
          <a href="<?php echo esc_url( get_category_link( $cat->term_id ) ); ?>" itemprop="item">
            <span itemprop="name"><?php echo esc_html( $cat->name ); ?></span>
          </a>
          <meta itemprop="position" content="<?php echo esc_attr( $position ); ?>" />
        </li>
    <?php
        $position++;
      endforeach;
    endif; ?>

    <!-- Current post (active) -->
    <li class="breadcrumb-item active" aria-current="page" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem">
      <span itemprop="name"><?php echo esc_html( get_the_title( $post->ID ) ); ?></span>
      <meta itemprop="position" content="<?php echo esc_attr( max( 2, ( isset( $position ) ? $position : 2 ) ) ); ?>" />
    </li>
  </ol>
</nav>