<?php
/**
 * File Path: templates/partials/coming-soon-badge.php
 *
 * My Book Showroom — Coming Soon Badge
 *
 * Renders a Coming Soon badge on books flagged as upcoming releases.
 * Closes PLG-005 (setting consumer) and PLG-010 (front-end renderer).
 *
 * Gate: mbs_setting_general_coming_soon_badge must be '1' (PRO-only setting).
 * Book-level: mbs_coming_soon postmeta must be '1'.
 *
 * Three variants (per spec):
 *   1. Bare        — mbs_coming_soon = '1', no publication date set.
 *                    Renders: "Coming Soon!"
 *   2. Dated       — mbs_coming_soon = '1', future publication date set.
 *                    Renders: "Coming Apr 22, 2026!"
 *   3. Dated + CTA — Variant 2 plus a populated mbs_preorder_link (PRO only).
 *                    Renders: "Coming Apr 22, 2026!" + "Pre-order Now →" button.
 *
 * GP-UI-1: nothing renders if the setting is off or the book is not flagged.
 *
 * Expected context variable (optional):
 *   int $post_id — defaults to get_the_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

if ( ! mbs_is_registered() ) {
	return;
}

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

// ── Gate 1: Global badge setting (PRO-only; defaults '0') ───────────────────
if ( ! (bool) mbs_get_setting( 'general', 'coming_soon_badge' ) ) {
	return;
}

// ── Gate 2: Book-level Coming Soon flag ─────────────────────────────────────
$coming_soon = (string) MBS_Book_Meta_DAO::get( $post_id, 'mbs_coming_soon', '' );
if ( '1' !== $coming_soon ) {
	return;
}

// ── Resolve publication date ─────────────────────────────────────────────────
// Stored as Y-m-d. Empty string when not set.
// GC2-3: Use MBS_Formatting::format_pub_date() so midnight is treated as local
// midnight in the MBS timezone — prevents UTC-offset sites from showing the
// wrong date (e.g. "Apr 30" instead of "May 1" for UTC-5 sites).
$pub_date_raw   = (string) get_post_meta( $post_id, 'mbs_pub_date', true );
$has_date       = ( '' !== $pub_date_raw );
$formatted_date = $has_date
	? MBS_Formatting::format_pub_date( $pub_date_raw )
	: '';

// ── Gate 3: Badge only renders when publication date is in the future ─────────
if ( $has_date ) {
	$today_str = wp_date( 'Y-m-d' ); // Respects site timezone (GC2-3).
	if ( $today_str >= $pub_date_raw ) {
		return; // Publication date is today or past — badge no longer applicable.
	}
}

// ── Resolve preorder link (PRO only — field is PRO-gated on save) ────────────
$preorder_url = ( mbs_is_pro() && $has_date )
	? (string) get_post_meta( $post_id, 'mbs_preorder_link', true )
	: '';

// ── Determine variant ────────────────────────────────────────────────────────
// Variant 3: dated + preorder link
// Variant 2: dated only
// Variant 1: bare (no date)
$variant = 1;
if ( $has_date && '' !== $preorder_url ) {
	$variant = 3;
} elseif ( $has_date ) {
	$variant = 2;
}
?>

<div class="mbs-wrapper mbs-coming-soon-badge mbs-coming-soon-variant-<?php echo esc_attr( $variant ); ?>">

	<span class="mbs-coming-soon-label">
		<?php if ( 1 === $variant ) : ?>
			<?php esc_html_e( 'Coming Soon!', 'my-book-showroom' ); ?>
		<?php else : ?>
			<?php
			printf(
				/* translators: %s: formatted release date, e.g. "Apr 22, 2026" */
				esc_html__( 'Coming %s!', 'my-book-showroom' ),
				esc_html( $formatted_date )
			);
			?>
		<?php endif; ?>
	</span>

	<?php if ( 3 === $variant ) : ?>
		<a href="<?php echo esc_url( $preorder_url ); ?>"
		   class="mbs-coming-soon-preorder button"
		   target="_blank"
		   rel="noopener noreferrer">
			<?php esc_html_e( 'Pre-order Now →', 'my-book-showroom' ); ?>
		</a>
	<?php endif; ?>

</div>
