<?php
/**
 * File Path: helpers/class-mbs-breadcrumbs.php
 *
 * Breadcrumbs Helper
 *
 * Generates breadcrumbs for book pages, author taxonomy, series taxonomy, etc.
 *
 */

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

class MBS_Breadcrumbs { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Render breadcrumbs HTML.
	 *
	 * Supports single book pages and all MBS taxonomy archive pages.
	 * Defers to Yoast SEO or RankMath when either is active (SV-09).
	 *
	 * @param int $post_id  Book post ID (used on singular pages only).
	 * @return string       Breadcrumb nav HTML, or empty string.
	 */
	public static function render( int $post_id = 0 ): string {
		if ( ! $post_id ) {
			$post_id = (int) get_the_ID();
		}

		// SV-09: Defer to Yoast SEO or RankMath when either is active.
		// Suppresses MBS breadcrumbs to prevent duplication on the page.
		if ( function_exists( 'yoast_breadcrumb' ) ) {
			return yoast_breadcrumb( '<nav class="mbs-breadcrumbs mbs-breadcrumbs--yoast" aria-label="Breadcrumb">', '</nav>', false );
		}
		if ( function_exists( 'rank_math_the_breadcrumbs' ) ) {
			ob_start();
			rank_math_the_breadcrumbs();
			return '<nav class="mbs-breadcrumbs mbs-breadcrumbs--rankmath" aria-label="Breadcrumb">' . ob_get_clean() . '</nav>';
		}

		ob_start();
		echo '<nav class="mbs-breadcrumbs" aria-label="Breadcrumb">';
		echo '<a href="' . esc_url( home_url() ) . '">' . esc_html__( 'Home', 'my-book-showroom' ) . '</a>';

		// ── Taxonomy archive pages ────────────────────────────────────────────
		$mbs_taxonomies = array(
			MBS_TAX_SERIES     => __( 'Series', 'my-book-showroom' ),
			MBS_TAX_AUTHOR     => __( 'Authors', 'my-book-showroom' ),
			MBS_TAX_GENRE      => __( 'Genre', 'my-book-showroom' ),
			'mbs_tag'          => __( 'Tags', 'my-book-showroom' ),
			'mbs_collection'   => __( 'Collections', 'my-book-showroom' ),
			'mbs_universe'     => __( 'Universe', 'my-book-showroom' ),
		);

		foreach ( $mbs_taxonomies as $tax => $label ) {
			if ( is_tax( $tax ) ) {
				$term = get_queried_object();
				echo ' &raquo; <span class="mbs-breadcrumbs__label">' . esc_html( $label ) . '</span>';
				if ( $term instanceof WP_Term ) {
					echo ' &raquo; <span aria-current="page">' . esc_html( $term->name ) . '</span>';
				}
				echo '</nav>';
				return ob_get_clean();
			}
		}

		// ── Single book page ──────────────────────────────────────────────────
		if ( is_singular( MBS_POST_TYPE ) ) {
			$terms = get_the_terms( $post_id, MBS_TAX_GENRE );
			if ( $terms && ! is_wp_error( $terms ) ) {
				$genre = array_shift( $terms );
				echo ' &raquo; <a href="' . esc_url( get_term_link( $genre ) ) . '">' . esc_html( $genre->name ) . '</a>';
			}
			echo ' &raquo; <span aria-current="page">' . esc_html( get_the_title( $post_id ) ) . '</span>';
		} else {
			echo ' &raquo; ' . esc_html__( 'Books', 'my-book-showroom' );
		}

		echo '</nav>';
		return ob_get_clean();
	}
}
