<?php
/**
 * File Path: templates/partials/per-format-details.php
 *
 * Per-Format Details Partial
 *
 * Renders the switchable per-format metadata block: subtitle, price, ISBN-13,
 * ISBN-10 (if setting enabled), page count / duration.
 *
 * JavaScript in mbs-format-tabs.js updates the inner spans on format switch.
 * The data attributes store initial values for the first (active) format; JS
 * reads them from the JSON payload on the .mbs-format-selector wrapper.
 *
 * GP-UI-1: Every field only renders when it has a value — no empty containers.
 *
 * @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;
}

if ( ! isset( $post_id ) ) {
	$post_id = get_the_ID();
}

$format_rows = MBS_Format_Row_DAO::get_by_post( $post_id );
if ( empty( $format_rows ) ) {
	return;
}

// Default to first (lowest sort_order) format row.
$first = $format_rows[0];

$price       = MBS_Formatting::format_price( (float) ( $first->price ?? 0 ) );
$isbn13      = MBS_ISBN_Helper::format_isbn( $first->isbn13 ?? '' );
$page_length = (int) ( $first->page_length ?? 0 );
$duration_hh = isset( $first->duration_hh ) && '' !== $first->duration_hh ? (int) $first->duration_hh : null;
$duration_mm = isset( $first->duration_mm ) && '' !== $first->duration_mm ? (int) $first->duration_mm : null;
$subtitle    = sanitize_text_field( $first->subtitle ?? '' );
$format_type = $first->format ?? '';

// P6-002: Master ISBN visibility toggle — gates both ISBN-13 and ISBN-10 display.
$show_isbn = (bool) mbs_get_setting( 'general', 'show_isbn' );

// ISBN-10 sub-toggle — only applies when show_isbn is on and ISBN starts with 978.
$show_isbn10 = (bool) mbs_get_setting( 'general', 'show_isbn10' );
$isbn10      = '';
if ( $show_isbn && $show_isbn10 && ! empty( $first->isbn13 ) ) {
	$isbn10 = MBS_ISBN_Helper::isbn13_to_isbn10( $first->isbn13 ) ?: '';
}

?>
<div class="mbs-wrapper mbs-per-format-details" id="mbs-per-format-details">

	<?php if ( $subtitle ) : ?>
	<div class="mbs-per-format-subtitle">
		<span id="mbs-format-subtitle"><?php echo esc_html( $subtitle ); ?></span>
	</div>
	<?php endif; ?>

	<dl class="mbs-per-format-meta">

		<?php if ( $price ) : ?>
		<div class="mbs-meta-row mbs-meta-price">
			<dt><?php esc_html_e( 'Price', 'my-book-showroom' ); ?></dt>
			<dd><span id="mbs-format-price"><?php echo esc_html( $price ); ?></span></dd>
		</div>
		<?php endif; ?>

		<?php if ( $show_isbn && $isbn13 ) : ?>
		<div class="mbs-meta-row mbs-meta-isbn13">
			<dt><?php esc_html_e( 'ISBN-13', 'my-book-showroom' ); ?></dt>
			<dd>
				<span id="mbs-format-isbn13"><?php echo esc_html( $isbn13 ); ?></span>
				<?php if ( $isbn10 ) : ?>
					<span class="mbs-isbn10-wrap"><?php esc_html_e( '(ISBN-10:', 'my-book-showroom' ); ?> <span id="mbs-format-isbn10"><?php echo esc_html( $isbn10 ); ?></span>)</span>
				<?php endif; ?>
			</dd>
		</div>
		<?php endif; ?>

		<?php if ( $page_length && $format_type !== 'audiobook' ) : ?>
		<div class="mbs-meta-row mbs-meta-page-length">
			<dt><?php esc_html_e( 'Pages', 'my-book-showroom' ); ?></dt>
			<dd><span id="mbs-format-page-length"><?php echo esc_html( $page_length ); ?></span></dd>
		</div>
		<?php endif; ?>

		<?php if ( $format_type === 'audiobook' && null !== $duration_hh ) : ?>
		<div class="mbs-meta-row mbs-meta-duration">
			<dt><?php esc_html_e( 'Duration', 'my-book-showroom' ); ?></dt>
			<dd>
				<span id="mbs-format-duration">
				<?php
				$hh_pad = str_pad( (string) $duration_hh, 2, '0', STR_PAD_LEFT );
				$mm_pad = str_pad( (string) ( $duration_mm ?? 0 ), 2, '0', STR_PAD_LEFT );
				echo esc_html( "{$hh_pad}:{$mm_pad}" );
				?>
				</span>
			</dd>
		</div>
		<?php endif; ?>

		<?php
		// F-021: Narrator — audiobook-only, GP-UI-1 (renders only when populated).
		$narrator = isset( $first->narrator ) ? (string) $first->narrator : '';
		if ( 'audiobook' === $format_type && '' !== $narrator ) :
		?>
		<div class="mbs-meta-row mbs-meta-narrator">
			<dt><?php esc_html_e( 'Narrator', 'my-book-showroom' ); ?></dt>
			<dd><span id="mbs-format-narrator"><?php echo esc_html( $narrator ); ?></span></dd>
		</div>
		<?php endif; ?>

	</dl>

</div>
