<?php
/**
 * Rental summary template
 *
 * Displays rental period info in cart, checkout, order confirmation
 * emails, and admin order view.
 *
 * Variables available:
 *   $rental_start (string) Y-m-d date string for the rental start
 *   $rental_end   (string) Y-m-d date string for the rental end
 *   $rental_days  (int)    number of rental days
 *
 * @package WooEquipmentRental
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- WC template variables injected via wc_get_template() extract

// All three values are required; bail silently if any are absent
if ( empty( $rental_start ) || empty( $rental_end ) || empty( $rental_days ) ) {
	return;
}

// Convert Y-m-d strings to timestamps for wp_date() formatting
$start_timestamp = strtotime( $rental_start );
$end_timestamp   = strtotime( $rental_end );

// Bail if either date string is unparseable
if ( false === $start_timestamp || false === $end_timestamp ) {
	return;
}

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

$start_formatted = wp_date( $date_format, $start_timestamp );
$end_formatted   = wp_date( $date_format, $end_timestamp );

// Bail if wp_date() failed (e.g. invalid timezone configuration)
if ( false === $start_formatted || false === $end_formatted ) {
	return;
}

$rental_days = absint( $rental_days );

$days_label = sprintf(
	/* translators: %d: number of days */
	_n( '%d day', '%d days', $rental_days, 'harbor-equipment-rental-for-woocommerce' ),
	$rental_days
);
?>
<dl class="wcrent-rental-summary-static">
	<dt><?php esc_html_e( 'Rental period:', 'harbor-equipment-rental-for-woocommerce' ); ?></dt>
	<dd>
		<span class="wcrent-rental-summary__dates">
			<?php
			echo esc_html( $start_formatted );
			echo ' &#8211; '; // en dash, HTML-encoded
			echo esc_html( $end_formatted );
			?>
		</span>
		<span class="wcrent-rental-summary__duration">
			<?php
			printf(
				/* translators: %s: number of days, e.g. "5 days" */
				'(%s)',
				esc_html( $days_label )
			);
			?>
		</span>
	</dd>
</dl>
