<?php
/**
 * File: includes/class-cc-booking-details.php
 * Purpose: Show booking details card on order-pay (checkout) and thankyou pages.
 */

defined('ABSPATH') || exit;

if ( ! function_exists('add_action') ) { return; }
if ( ! function_exists('wc_get_order') ) { return; } // WooCommerce required

/**
 * WHY: Tek yerde üretilen kutuyu iki farklı hook'ta kullanıyoruz.
 */
function cc_execvitr_render_booking_details_card( WC_Order $order ): void {
	$esc = static function( $v ) { return $v === null ? '' : esc_html( (string) $v ); };

	$ref_no        = $order->get_order_number();
	$total_html    = $order->get_formatted_order_total();
	$created       = $order->get_date_created() ? $order->get_date_created()->date_i18n( get_option('date_format').' '.get_option('time_format') ) : '';

	// Plugin meta keys (gerektiğinde burayı kendi anahtarlarınıza uyarlayın)
	$origin        = (string) $order->get_meta('_cc_address_from');
	$destination   = (string) $order->get_meta('_cc_address_to');
	$distance_text = (string) $order->get_meta('_cc_distance_text');     // örn: "45 km"
	$duration_text = (string) $order->get_meta('_cc_duration');          // örn: "55 dk"
	$passengers    = $order->get_meta('_cc_passenger_count');
	$luggage       = $order->get_meta('_cc_luggage_count');
	$pickup_at     = (string) $order->get_meta('_cc_pickup_datetime');   // varsa

	?>
	<style>
		.cc-bdet-card{border:1px solid #e5e7eb;border-radius:12px;background:#fff;margin:0 0 18px;padding:16px}
		.cc-bdet-h{margin:0 0 10px;font-size:18px}
		.cc-bdet-row{display:flex;justify-content:space-between;gap:8px;margin:6px 0}
		.cc-bdet-k{color:#6b7280}
	</style>
	<div class="cc-bdet-card">
		<h3 class="cc-bdet-h"><?php echo esc_html__('Reservation Summary', 'executive-vip-transfer-service'); ?></h3>
		<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Order', 'executive-vip-transfer-service'); ?></span><span>#<?php echo $esc($ref_no); ?></span></div>
		<?php if ($created): ?>
			<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Created', 'executive-vip-transfer-service'); ?></span><span><?php echo $esc($created); ?></span></div>
		<?php endif; ?>
		<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Total', 'executive-vip-transfer-service'); ?></span><span><?php echo wp_kses_post($total_html); ?></span></div>
		<?php if ($pickup_at): ?>
			<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Pickup Time', 'executive-vip-transfer-service'); ?></span><span><?php echo $esc($pickup_at); ?></span></div>
		<?php endif; ?>
		<?php if ($origin || $destination): ?>
			<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Route', 'executive-vip-transfer-service'); ?></span><span><?php echo $esc($origin); ?> → <?php echo $esc($destination); ?></span></div>
		<?php endif; ?>
		<?php if ($distance_text): ?>
			<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Distance', 'executive-vip-transfer-service'); ?></span><span><?php echo $esc($distance_text); ?></span></div>
		<?php endif; ?>
		<?php if ($duration_text): ?>
			<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Duration', 'executive-vip-transfer-service'); ?></span><span><?php echo $esc($duration_text); ?></span></div>
		<?php endif; ?>
		<?php if ($passengers): ?>
			<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Passengers', 'executive-vip-transfer-service'); ?></span><span><?php echo (int) $passengers; ?></span></div>
		<?php endif; ?>
		<?php if ($luggage): ?>
			<div class="cc-bdet-row"><span class="cc-bdet-k"><?php echo esc_html__('Luggage', 'executive-vip-transfer-service'); ?></span><span><?php echo (int) $luggage; ?></span></div>
		<?php endif; ?>
	</div>
	<?php
}

/**
 * order-pay (checkout ödeme) üstünde göster.
 */
add_action('woocommerce_before_pay', function( $order_id ) {
	$order = wc_get_order( $order_id );
	if ( ! $order instanceof WC_Order ) { return; }
	cc_execvitr_render_booking_details_card( $order );
}, 10, 1);

/**
 * Ödeme sonrası thankyou sayfasında da göster.
 */
add_action('woocommerce_thankyou', function( $order_id ) {
	$order = wc_get_order( $order_id );
	if ( ! $order instanceof WC_Order ) { return; }
	cc_execvitr_render_booking_details_card( $order );
}, 25, 1);
