<?php
/**
 * templates/summary.php
 * Booking summary page (execvitr-summary)
 *
 * NEW FLOW: booking -> summary -> order-pay
 * This page reads details from a WooCommerce order (created at booking time)
 * and provides a single CTA to the WooCommerce order-pay page.
 */

if (!defined('ABSPATH')) { exit; }

if (!function_exists('wc_get_order')) {
	echo '<p>' . esc_html__('WooCommerce is not available.', 'executive-vip-transfer-service') . '</p>';
	return;
}

$order_id = isset($_GET['order_id']) ? absint($_GET['order_id']) : 0;
$key      = isset($_GET['key']) ? sanitize_text_field(wp_unslash($_GET['key'])) : '';

$order = $order_id ? wc_get_order($order_id) : null;
if (!$order || !is_a($order, 'WC_Order')) {
	status_header(404);
	echo '<p>' . esc_html__('Order not found.', 'executive-vip-transfer-service') . '</p>';
	return;
}

// Basic security: order key must match
if ($key === '' || $key !== $order->get_order_key()) {
	status_header(403);
	echo '<p>' . esc_html__('Invalid order key.', 'executive-vip-transfer-service') . '</p>';
	return;
}

// Ensure it is our booking flow order (best-effort)
$is_our = (bool) $order->get_meta('_execvitr_booking_flow');
if (!$is_our && function_exists('execvitr_is_our_order')) {
	$is_our = (bool) execvitr_is_our_order($order);
}
if (!$is_our) {
	status_header(403);
	echo '<p>' . esc_html__('This order is not eligible for summary.', 'executive-vip-transfer-service') . '</p>';
	return;
}

// Ensure meta is present (compat helper in main plugin)
if (function_exists('execvitr_ensure_order_booking_meta')) {
	execvitr_ensure_order_booking_meta($order);
}

$pickup        = (string) ($order->get_meta('_execvitr_pickup') ?: $order->get_meta('_cc_address_from'));
$drop          = (string) ($order->get_meta('_execvitr_drop') ?: $order->get_meta('_cc_address_to'));
$pickup_dt     = (string) ($order->get_meta('_execvitr_date') ?: $order->get_meta('_cc_pickup_datetime'));
$distance_text = (string) ($order->get_meta('_cc_distance_text'));
$duration_text = (string) ($order->get_meta('_cc_duration'));
$total_html    = $order->get_formatted_order_total();

$booking_json  = (string) $order->get_meta('_execvitr_booking_json');
$booking       = [];
if ($booking_json) {
	$decoded = json_decode($booking_json, true);
	if (is_array($decoded)) $booking = $decoded;
}

$car_name      = isset($booking['car_name']) ? (string) $booking['car_name'] : '';
$passengers    = isset($booking['passengers']) ? (int) $booking['passengers'] : 0;
$luggage       = isset($booking['luggage']) ? (int) $booking['luggage'] : 0;

get_header();
?>
<div class="execvitr-summary-wrap" style="max-width: 980px; margin: 0 auto; padding: 28px 18px;">
	<h1 style="margin: 0 0 18px;"><?php echo esc_html__('Reservation Summary', 'executive-vip-transfer-service'); ?></h1>

	<div style="background:#fff; border:1px solid rgba(0,0,0,.08); border-radius: 12px; padding: 16px 16px; box-shadow: 0 6px 18px rgba(0,0,0,.06);">
		<ul style="margin:0; padding-left: 18px;">
			<?php if ($pickup !== ''): ?><li><strong><?php echo esc_html__('Pickup:', 'executive-vip-transfer-service'); ?></strong> <?php echo esc_html($pickup); ?></li><?php endif; ?>
			<?php if ($drop !== ''): ?><li><strong><?php echo esc_html__('Drop-off:', 'executive-vip-transfer-service'); ?></strong> <?php echo esc_html($drop); ?></li><?php endif; ?>
			<?php if ($pickup_dt !== ''): ?><li><strong><?php echo esc_html__('Pickup Date/Time:', 'executive-vip-transfer-service'); ?></strong> <?php echo esc_html($pickup_dt); ?></li><?php endif; ?>
			<?php if ($distance_text !== ''): ?><li><strong><?php echo esc_html__('Distance:', 'executive-vip-transfer-service'); ?></strong> <?php echo esc_html($distance_text); ?></li><?php endif; ?>
			<?php if ($duration_text !== ''): ?><li><strong><?php echo esc_html__('Estimated Duration:', 'executive-vip-transfer-service'); ?></strong> <?php echo esc_html($duration_text); ?></li><?php endif; ?>
			<?php if ($car_name !== ''): ?><li><strong><?php echo esc_html__('Car:', 'executive-vip-transfer-service'); ?></strong> <?php echo esc_html($car_name); ?></li><?php endif; ?>
			<?php if ($passengers > 0): ?><li><strong><?php echo esc_html__('Passengers:', 'executive-vip-transfer-service'); ?></strong> <?php echo esc_html((string)$passengers); ?></li><?php endif; ?>
			<?php if ($luggage > 0): ?><li><strong><?php echo esc_html__('Luggage:', 'executive-vip-transfer-service'); ?></strong> <?php echo esc_html((string)$luggage); ?></li><?php endif; ?>
			<li><strong><?php echo esc_html__('Total:', 'executive-vip-transfer-service'); ?></strong> <?php echo wp_kses_post($total_html); ?></li>
		</ul>
	</div>

	<div style="display:flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-top: 14px;">
		<a class="button alt" href="<?php echo esc_url($order->get_checkout_payment_url()); ?>" style="padding: 12px 18px;">
			<?php echo esc_html__('Proceed to Payment', 'executive-vip-transfer-service'); ?>
		</a>
	</div>
</div>
<?php
get_footer();
