<?php
/**
 * File Path: templates/partials/section-book-details.php
 *
 * My Book Showroom - Book Details Section
 *
 * FREE tier. Renders publisher, word count, universe, collections,
 * genre, tags, themes, content warnings, BISAC, and Thema for a single book.
 *
 * GP-UI-1: Upfront boolean scan — returns with zero output when all
 * fields are empty (SS-12b). Does not use output buffering.
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variables injected by mbs_get_template_part(); not global vars

if ( ! mbs_is_registered() ) {
	return;
}

$post_id = isset( $post_id ) ? (int) $post_id : get_the_ID();

// --- Read all meta + taxonomy values upfront (SS-12b) ---
$publisher     = get_post_meta( $post_id, 'mbs_publisher',     true );
$publisher_url = get_post_meta( $post_id, 'mbs_publisher_url', true );
$word_count    = (int) MBS_Book_Meta_DAO::get( $post_id, 'mbs_word_count', 0 );
$show_wc       = (bool) get_post_meta( $post_id, 'mbs_show_word_count', true );
$show_bisac    = ( '1' === get_post_meta( $post_id, 'mbs_show_bisac_public', true ) );
$show_thema    = ( '1' === get_post_meta( $post_id, 'mbs_show_thema_public', true ) );
$show_universe = ( mbs_is_pro() && (int) get_option( 'mbs_setting_advanced_universe_enabled', 0 ) );

$collections = wp_get_post_terms( $post_id, MBS_TAX_COLLECTION );
$collections = ( ! is_wp_error( $collections ) && ! empty( $collections ) ) ? $collections : array();

$genres   = wp_get_post_terms( $post_id, MBS_TAX_GENRE );
$tags     = wp_get_post_terms( $post_id, MBS_TAX_TAG );
$themes   = wp_get_post_terms( $post_id, MBS_TAX_THEME );
$warnings = wp_get_post_terms( $post_id, MBS_TAX_CONTENT_WARNING );
$genres   = ( ! is_wp_error( $genres )   && ! empty( $genres ) )   ? $genres   : array();
$tags     = ( ! is_wp_error( $tags )     && ! empty( $tags ) )     ? $tags     : array();
$themes   = ( ! is_wp_error( $themes )   && ! empty( $themes ) )   ? $themes   : array();
$warnings = ( ! is_wp_error( $warnings ) && ! empty( $warnings ) ) ? $warnings : array();

$bisac_list = $show_bisac ? MBS_Classification_DAO::get_book_codes( $post_id, 'bisac' ) : array();
$thema_list = $show_thema ? MBS_Classification_DAO::get_book_codes( $post_id, 'thema' ) : array();

$universes = array();
if ( $show_universe ) {
	$universes = wp_get_post_terms( $post_id, MBS_TAX_UNIVERSE );
	$universes = ( ! is_wp_error( $universes ) && ! empty( $universes ) ) ? $universes : array();
}

// GP-UI-1: suppress section entirely when nothing is displayable.
$show_genre_public = ( $genres && '1' === get_post_meta( $post_id, 'mbs_show_genre_public', true ) );
if (
	! $publisher
	&& ! ( $show_wc && $word_count > 0 )
	&& ! $universes
	&& ! $collections
	&& ! $show_genre_public
	&& ! $tags
	&& ! $themes
	&& ! $warnings
	&& ! $bisac_list
	&& ! $thema_list
) {
	return;
}
?>

<div class="mbs-wrapper mbs-section mbs-section-book-details">

	<?php if ( $publisher ) : ?>
		<p class="mbs-publisher">
			<strong><?php esc_html_e( 'Publisher:', 'my-book-showroom' ); ?></strong>
			<?php if ( $publisher_url ) : ?>
				<a href="<?php echo esc_url( $publisher_url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $publisher ); ?></a>
			<?php else : ?>
				<?php echo esc_html( $publisher ); ?>
			<?php endif; ?>
		</p>
	<?php endif; ?>

	<?php
	// F-022: Word count + reading time estimate. Admin-opt-in per book.
	if ( $show_wc && $word_count > 0 ) :
		$reading_minutes = (int) round( $word_count / 250 );
		$reading_hours   = intdiv( $reading_minutes, 60 );
		$reading_mins_r  = $reading_minutes % 60;
		?>
		<p class="mbs-word-count">
			<strong><?php esc_html_e( 'Word Count:', 'my-book-showroom' ); ?></strong>
			<?php echo esc_html( number_format( $word_count ) ); ?>
			<?php if ( $reading_hours > 0 || $reading_mins_r > 0 ) : ?>
				<span class="mbs-reading-time">
					&mdash;
					<?php
					if ( $reading_hours > 0 && $reading_mins_r > 0 ) {
						printf(
							/* translators: 1: hours, 2: minutes */
							esc_html__( 'about %1$dh %2$dm read', 'my-book-showroom' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_html__() escapes format string; integer time values are safe with %d
							$reading_hours, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- integer reading time value
							$reading_mins_r // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- integer reading time value
						);
					} elseif ( $reading_hours > 0 ) {
						printf(
							/* translators: %d: hours */
							esc_html__( 'about %dh read', 'my-book-showroom' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_html__() escapes format string; integer time values are safe with %d
							$reading_hours // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- integer reading time value
						);
					} else {
						printf(
							/* translators: %d: minutes */
							esc_html__( 'about %d min read', 'my-book-showroom' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- esc_html__() escapes format string; integer time values are safe with %d
							$reading_minutes // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- integer reading time value
						);
					}
					?>
				</span>
			<?php endif; ?>
		</p>
	<?php endif; ?>

	<?php
	// F-017: Universe display. PRO + opt-in toggle required.
	if ( $universes ) :
		$uni_links = array();
		foreach ( $universes as $uni ) {
			$uni_links[] = '<a href="' . esc_url( get_term_link( $uni ) ) . '" class="mbs-universe-link">'
			             . esc_html( $uni->name )
			             . '</a>';
		}
		?>
		<p class="mbs-universes mbs-secondary-meta">
			<span class="mbs-universes-label"><?php esc_html_e( 'Universe:', 'my-book-showroom' ); ?></span>
			<?php echo implode( ' &middot; ', $uni_links ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- each link individually escaped above ?>
		</p>
	<?php endif; ?>

	<?php
	// F-016: Collections display.
	if ( $collections ) :
		$col_links = array();
		foreach ( $collections as $col ) {
			$col_links[] = '<a href="' . esc_url( get_term_link( $col ) ) . '" class="mbs-collection-link">'
			             . esc_html( $col->name )
			             . '</a>';
		}
		?>
		<p class="mbs-collections">
			<strong><?php esc_html_e( 'Part of:', 'my-book-showroom' ); ?></strong>
			<?php echo implode( ' &middot; ', $col_links ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- each link individually escaped above ?>
		</p>
	<?php endif; ?>

	<?php
	// F-019: Tertiary categorization — Genre, Tags, Themes, Content Warnings.
	if ( $genres || $tags || $themes || $warnings ) :
		?>
		<div class="mbs-categorization">

			<?php if ( $show_genre_public ) :
				$genre_links = array();
				foreach ( $genres as $g ) {
					$genre_links[] = '<a href="' . esc_url( get_term_link( $g ) ) . '" class="mbs-genre-link">'
					               . esc_html( $g->name ) . '</a>';
				}
				?>
				<p class="mbs-genres mbs-cat-row">
					<span class="mbs-cat-label"><?php esc_html_e( 'Genre:', 'my-book-showroom' ); ?></span>
					<span class="mbs-cat-terms"><?php echo implode( ' &middot; ', $genre_links ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
				</p>
			<?php endif; ?>

			<?php if ( $warnings ) :
				$warning_items = array();
				foreach ( $warnings as $w ) {
					$warning_items[] = '<span class="mbs-warning-item">' . esc_html( $w->name ) . '</span>';
				}
				?>
				<p class="mbs-content-warnings mbs-cat-row">
					<span class="mbs-cat-label"><?php esc_html_e( 'Content Warnings:', 'my-book-showroom' ); ?></span>
					<span class="mbs-cat-terms mbs-cat-terms--warnings"><?php echo implode( ' &middot; ', $warning_items ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
				</p>
			<?php endif; ?>

			<?php if ( $tags ) :
				$tag_links = array();
				foreach ( $tags as $t ) {
					$tag_links[] = '<a href="' . esc_url( get_term_link( $t ) ) . '" class="mbs-tag-link">'
					             . esc_html( $t->name ) . '</a>';
				}
				?>
				<p class="mbs-tags mbs-cat-row">
					<span class="mbs-cat-label"><?php esc_html_e( 'Tags:', 'my-book-showroom' ); ?></span>
					<span class="mbs-cat-terms"><?php echo implode( ' &middot; ', $tag_links ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
				</p>
			<?php endif; ?>

			<?php if ( $themes ) :
				$theme_links = array();
				foreach ( $themes as $th ) {
					$theme_links[] = '<a href="' . esc_url( get_term_link( $th ) ) . '" class="mbs-theme-link">'
					               . esc_html( $th->name ) . '</a>';
				}
				?>
				<p class="mbs-themes mbs-cat-row">
					<span class="mbs-cat-label"><?php esc_html_e( 'Themes:', 'my-book-showroom' ); ?></span>
					<span class="mbs-cat-terms"><?php echo implode( ' &middot; ', $theme_links ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
				</p>
			<?php endif; ?>

		</div>
	<?php endif; ?>

	<?php
	// Sprint CLASSIFICATION: BISAC codes display.
	if ( $bisac_list ) :
		$_bisac_dao   = new MBS_Classification_DAO();
		$_bisac_descs = $_bisac_dao->get_descriptions_batch( 'bisac', $bisac_list );
		$bisac_items  = array();
		foreach ( $bisac_list as $code ) {
			$code          = esc_html( $code );
			$label         = isset( $_bisac_descs[ $code ] ) ? ' &mdash; ' . esc_html( $_bisac_descs[ $code ] ) : '';
			$bisac_items[] = '<span class="mbs-bisac-item">' . $code . $label . '</span>';
		}
		?>
		<div class="mbs-classification-meta mbs-bisac-meta">
			<p class="mbs-bisac-row mbs-cat-row">
				<span class="mbs-cat-label"><?php esc_html_e( 'BISAC:', 'my-book-showroom' ); ?></span>
				<span class="mbs-cat-terms"><?php echo implode( ' &middot; ', $bisac_items ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
			</p>
		</div>
	<?php endif; ?>

	<?php
	// Sprint CLASSIFICATION: Thema codes display.
	if ( $thema_list ) :
		$thema_items = array();
		foreach ( $thema_list as $code ) {
			$thema_items[] = '<span class="mbs-thema-item">' . esc_html( $code ) . '</span>';
		}
		?>
		<div class="mbs-classification-meta mbs-thema-meta">
			<p class="mbs-thema-row mbs-cat-row">
				<span class="mbs-cat-label"><?php esc_html_e( 'Thema:', 'my-book-showroom' ); ?></span>
				<span class="mbs-cat-terms"><?php echo implode( ' &middot; ', $thema_items ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
			</p>
		</div>
	<?php endif; ?>

</div>
