<?php
/**
 * Order Summary table — shared by all reservation emails.
 * Expects in scope: $reservation, $settings, $l (language strings).
 */
if ( ! defined( 'ABSPATH' ) ) exit;

$bingocr_days = max( 1, (int) ( ( strtotime( $reservation['return_date'] ) - strtotime( $reservation['pickup_date'] ) ) / DAY_IN_SECONDS ) );

$bingocr_extra_ids = json_decode( $reservation['extras'] ?? '[]', true );
if ( ! is_array( $bingocr_extra_ids ) ) {
	$bingocr_extra_ids = array();
}
$bingocr_extras       = array();
$bingocr_extras_total = 0.0;
foreach ( $bingocr_extra_ids as $bingocr_eid ) {
	$bingocr_extra = BingoCR_DB::get_extra( (int) $bingocr_eid );
	if ( $bingocr_extra ) {
		$bingocr_amt = $bingocr_extra['price_type'] === 'per_day'
			? (float) $bingocr_extra['price'] * $bingocr_days
			: (float) $bingocr_extra['price'];
		$bingocr_extras[]      = array( 'name' => $bingocr_extra['name'], 'amount' => $bingocr_amt );
		$bingocr_extras_total += $bingocr_amt;
	}
}

// Out-of-hours fee, recomputed from the stored times + current settings.
$bingocr_ooh = BingoCR_Pricing::out_of_hours_fee(
	substr( (string) ( $reservation['pickup_time'] ?? '' ), 0, 5 ),
	substr( (string) ( $reservation['return_time'] ?? '' ), 0, 5 )
);
$bingocr_ooh_fee = (float) $bingocr_ooh['fee'];

$bingocr_total    = (float) $reservation['total_price'];
$bingocr_discount = (float) ( $reservation['discount_amount'] ?? 0 );
$bingocr_base     = $bingocr_total - $bingocr_extras_total - $bingocr_ooh_fee + $bingocr_discount;
$bingocr_deposit  = (float) ( $reservation['deposit_amount'] ?? 0 );

// Split the base into pre-seasonal + seasonal adjustment by recomputing the
// seasonal portion through the engine. Derived so pre + seasonal == stored base.
$bingocr_seasonal      = 0.0;
$bingocr_seasonal_segs = array();
$bingocr_car           = BingoCR_DB::get_car( (int) ( $reservation['car_id'] ?? 0 ) );
if ( $bingocr_car ) {
	$bingocr_calc          = BingoCR_Pricing::calculate( $bingocr_car, $reservation['pickup_date'], $reservation['return_date'] );
	$bingocr_seasonal      = (float) ( $bingocr_calc['seasonal_adj'] ?? 0 );
	$bingocr_seasonal_segs = $bingocr_calc['seasonal_breakdown'] ?? array();
}
$bingocr_base_pre = $bingocr_base - $bingocr_seasonal;

$bingocr_base_label = $bingocr_days === 1
	? $l['base_rental_one']
	: sprintf( $l['base_rental'], $bingocr_days );
?>
<div class="section-title"><?php echo esc_html( $l['order_summary'] ); ?></div>
<table>
  <tr>
    <td><?php echo esc_html( $bingocr_base_label ); ?></td>
    <td><?php echo esc_html( BingoCR_Email::format_price( $bingocr_base_pre, $settings ) ); ?></td>
  </tr>
  <?php if ( ! empty( $bingocr_seasonal_segs ) ) : ?>
    <?php foreach ( $bingocr_seasonal_segs as $bingocr_seg ) : ?>
      <?php if ( abs( (float) $bingocr_seg['amount'] ) < 0.001 ) { continue; } ?>
      <?php
		$bingocr_seg_mod   = (float) $bingocr_seg['modifier'];
		$bingocr_seg_modstr = ( $bingocr_seg_mod > 0 ? '+' : '' ) . rtrim( rtrim( number_format( $bingocr_seg_mod, 2 ), '0' ), '.' ) . '%';
		$bingocr_seg_nword = ( (int) $bingocr_seg['nights'] === 1 ) ? $l['day'] : $l['days'];
		$bingocr_seg_name  = $bingocr_seg['name'] !== '' ? $bingocr_seg['name'] : $l['seasonal'];
		$bingocr_seg_label = $l['seasonal'] . ' · ' . $bingocr_seg_name . ' (' . (int) $bingocr_seg['nights'] . ' ' . $bingocr_seg_nword . ', ' . $bingocr_seg_modstr . ')';
      ?>
  <tr>
    <td><?php echo esc_html( $bingocr_seg_label ); ?></td>
    <td><?php echo ( (float) $bingocr_seg['amount'] >= 0 ? '+ ' : '- ' ) . esc_html( BingoCR_Email::format_price( abs( (float) $bingocr_seg['amount'] ), $settings ) ); ?></td>
  </tr>
    <?php endforeach; ?>
  <?php elseif ( abs( $bingocr_seasonal ) > 0.001 ) : ?>
  <tr>
    <td><?php echo esc_html( $l['seasonal'] ); ?></td>
    <td><?php echo ( $bingocr_seasonal >= 0 ? '+ ' : '- ' ) . esc_html( BingoCR_Email::format_price( abs( $bingocr_seasonal ), $settings ) ); ?></td>
  </tr>
  <?php endif; ?>
  <?php foreach ( $bingocr_extras as $bingocr_x ) : ?>
  <tr>
    <td>+ <?php echo esc_html( $bingocr_x['name'] ); ?></td>
    <td><?php echo esc_html( BingoCR_Email::format_price( $bingocr_x['amount'], $settings ) ); ?></td>
  </tr>
  <?php endforeach; ?>
  <?php if ( $bingocr_ooh_fee > 0 ) : ?>
  <tr>
    <td><?php echo esc_html( $l['out_of_hours_fee'] ); ?></td>
    <td>+ <?php echo esc_html( BingoCR_Email::format_price( $bingocr_ooh_fee, $settings ) ); ?></td>
  </tr>
  <?php endif; ?>
  <?php if ( $bingocr_discount > 0 ) : ?>
  <tr>
    <td><?php echo esc_html( $l['discount'] ); ?><?php echo ! empty( $reservation['discount_code'] ) ? ' (' . esc_html( $reservation['discount_code'] ) . ')' : ''; ?></td>
    <td>- <?php echo esc_html( BingoCR_Email::format_price( $bingocr_discount, $settings ) ); ?></td>
  </tr>
  <?php endif; ?>
  <tr class="total-row">
    <td><?php echo esc_html( $l['total'] ); ?></td>
    <td><?php echo esc_html( BingoCR_Email::format_price( $bingocr_total, $settings ) ); ?></td>
  </tr>
  <?php if ( $bingocr_deposit > 0 ) : ?>
  <tr class="deposit-row">
    <td><?php echo esc_html( $l['deposit'] ); ?></td>
    <td><?php echo esc_html( BingoCR_Email::format_price( $bingocr_deposit, $settings ) ); ?></td>
  </tr>
  <?php endif; ?>
</table>
