<?php
/**
 * File Path: templates/partials/timeline.php
 *
 * MBS Publication Timeline Partial -- DISPLAY-1
 *
 * Renders a vertical `<ol class="mbs-timeline">` with one entry per format
 * row plus chronologically-placed ARC date entries.
 *
 * Variables passed via mbs_get_template_part() $args:
 *   $format_rows array   -- non-empty array of mbs_format_rows stdClass objects.
 *   $arc_data    array   -- keys: open_date, close_date, deadline (strings, may be empty).
 *   $book_id     int     -- the mbs_book post ID.
 *
 * @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

// Variable extraction (explicit, no extract()).
$format_rows = $args['format_rows'] ?? array();
$arc_data    = $args['arc_data']    ?? array();
$book_id     = (int) ( $args['book_id'] ?? 0 );

if ( empty( $format_rows ) ) {
	return;
}

$date_format = mbs_get_setting( 'general', 'date_format', get_option( 'date_format', 'F j, Y' ) );

// ── Build timeline entries ────────────────────────────────────────────────────
$entries = array();

foreach ( $format_rows as $row ) {
	$pub_date_raw = (string) ( $row->pub_date ?? '' );
	$pub_ts       = $pub_date_raw ? strtotime( $pub_date_raw ) : 0;
	$pub_label    = ( $pub_ts > 0 )
		? date_i18n( $date_format, $pub_ts )
		: esc_html__( 'Date TBD', 'my-book-showroom' );

	$format_label = ! empty( $row->format_label )
		? (string) $row->format_label
		: (string) ( $row->format ?? 'Edition' );

	$entries[] = array(
		'type'    => 'format',
		'sort_ts' => $pub_ts ?: PHP_INT_MAX,
		'label'   => $format_label,
		'date'    => $pub_label,
		'raw'     => $pub_date_raw,
	);
}

// ── Add ARC entries ───────────────────────────────────────────────────────────
$arc_map = array(
	'open_date'  => __( 'ARC Opens',        'my-book-showroom' ),
	'close_date' => __( 'ARC Closes',       'my-book-showroom' ),
	'deadline'   => __( 'Review Deadline',  'my-book-showroom' ),
);

foreach ( $arc_map as $key => $arc_label ) {
	$raw = (string) ( $arc_data[ $key ] ?? '' );
	if ( '' === $raw ) {
		continue;
	}
	$ts = strtotime( $raw );
	if ( ! $ts ) {
		continue;
	}
	$entries[] = array(
		'type'    => 'arc',
		'sort_ts' => $ts,
		'label'   => $arc_label,
		'date'    => date_i18n( $date_format, $ts ),
		'raw'     => $raw,
	);
}

// ── Sort chronologically ──────────────────────────────────────────────────────
usort( $entries, static function ( array $a, array $b ): int {
	return $a['sort_ts'] <=> $b['sort_ts'];
} );

?>
<div class="mbs-timeline-wrap">
	<ol class="mbs-timeline" aria-label="<?php esc_attr_e( 'Publication Timeline', 'my-book-showroom' ); ?>">
		<?php foreach ( $entries as $entry ) : ?>
		<li class="mbs-timeline-entry mbs-timeline-<?php echo esc_attr( $entry['type'] ); ?>">
			<span class="mbs-timeline-dot" aria-hidden="true"></span>
			<div class="mbs-timeline-content">
				<strong class="mbs-timeline-label"><?php echo esc_html( $entry['label'] ); ?></strong>
				<time class="mbs-timeline-date"
				      datetime="<?php echo esc_attr( $entry['raw'] ); ?>"><?php echo esc_html( $entry['date'] ); ?></time>
			</div>
		</li>
		<?php endforeach; ?>
	</ol>
</div>
