<?php
/**
 * ============================================
 * FILE: templates/emails/admin/new-meeting.php
 * Admin New Meeting Email Template
 * ============================================
 *
 * @package RadiusTheme\RadiusBooking
 *
 * @var \RadiusTheme\RadiusBooking\Abstracts\BaseEmail $email
 * @var array                                          $booking
 * @var array                                          $settings
 * @var array                                          $merge_tags
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

do_action( 'rtrb_get_email_header', $email, $booking );
do_action( 'rtrb_email_before_content', $email, $booking );
do_action( 'rtrb_email_content_start', $email, $booking );
?>

<!-- Email Content Text -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td class="main-content">
			<?php if ( ! empty( $settings['additional_content'] ) ) : ?>
				<?php echo wp_kses_post( wpautop( wptexturize( $settings['additional_content'] ) ) ); ?>
			<?php else : ?>
				<p style="margin: 0;"><?php esc_html_e( 'A new meeting booking has been created in the system. Please review the details below and take necessary action.', 'radius-booking' ); ?></p>
			<?php endif; ?>
		</td>
	</tr>
</table>

<!-- Quick Stats Box -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background: linear-gradient(135deg, #ecfeff 0%, #cffafe 100%); border: 1px solid #0891b2; border-radius: 6px; margin: 16px 0;">
	<tr>
		<td style="padding: 16px;">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="50%" style="padding: 8px; text-align: center; border-right: 1px solid #67e8f9;">
						<p style="margin: 0 0 4px 0; font-size: 12px; color: #0e7490; font-weight: 500;">
							<?php esc_html_e( 'Booking ID', 'radius-booking' ); ?>
						</p>
						<p style="margin: 0; font-size: 18px; color: #164e63; font-weight: bold;">
							#<?php echo esc_html( $booking['customer']->id ?? '' ); ?>
						</p>
					</td>
					<td width="50%" style="padding: 8px; text-align: center;">
						<p style="margin: 0 0 4px 0; font-size: 12px; color: #0e7490; font-weight: 500;">
							<?php esc_html_e( 'Meeting Type', 'radius-booking' ); ?>
						</p>
						<p style="margin: 0; font-size: 16px; color: #164e63; font-weight: bold;">
							<?php echo esc_html( $merge_tags['meeting_type_name'] ?? '—' ); ?>
						</p>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>

<!-- Customer Information Box -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #f9fafb; border: 1px solid #e5e7eb; border-radius: 6px; margin: 16px 0;">
	<tr>
		<td style="padding: 16px;">
			<p style="margin: 0 0 12px 0; font-weight: 600; color: #111827; font-size: 15px; border-bottom: 2px solid #e5e7eb; padding-bottom: 8px;">
				<?php esc_html_e( 'Customer Information', 'radius-booking' ); ?>
			</p>
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td style="padding: 4px 0;">
						<p style="margin: 0; font-size: 14px; color: #374151;">
							<strong style="color: #6b7280;"><?php esc_html_e( 'Name:', 'radius-booking' ); ?></strong>
							<?php echo esc_html( $booking['customer']->customer_info['name'] ?? '' ); ?>
						</p>
					</td>
				</tr>
				<tr>
					<td style="padding: 4px 0;">
						<p style="margin: 0; font-size: 14px; color: #374151;">
							<strong style="color: #6b7280;"><?php esc_html_e( 'Email:', 'radius-booking' ); ?></strong>
							<a href="mailto:<?php echo esc_attr( $booking['customer']->customer_info['email'] ?? '' ); ?>" style="color: #2563eb; text-decoration: none;">
								<?php echo esc_html( $booking['customer']->customer_info['email'] ?? '' ); ?>
							</a>
						</p>
					</td>
				</tr>
				<?php if ( ! empty( $booking['customer']->customer_info['phone'] ) ) : ?>
					<tr>
						<td style="padding: 4px 0;">
							<p style="margin: 0; font-size: 14px; color: #374151;">
								<strong style="color: #6b7280;"><?php esc_html_e( 'Phone:', 'radius-booking' ); ?></strong>
								<a href="tel:<?php echo esc_attr( $booking['customer']->customer_info['phone'] ); ?>" style="color: #2563eb; text-decoration: none;">
									<?php echo esc_html( $booking['customer']->customer_info['phone'] ); ?>
								</a>
							</p>
						</td>
					</tr>
				<?php endif; ?>
			</table>
		</td>
	</tr>
</table>

<!-- Host Box -->
<?php if ( ! empty( $booking['employee_name'] ) ) : ?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #fef3c7; border: 1px solid #fbbf24; border-radius: 6px; margin: 16px 0;">
		<tr>
			<td style="padding: 16px;">
				<p style="margin: 0 0 8px 0; font-weight: 600; color: #78350f; font-size: 14px;">
					<?php esc_html_e( 'Meeting Host:', 'radius-booking' ); ?>
				</p>
				<p style="margin: 0; font-size: 14px; color: #92400e;">
					<?php echo esc_html( $booking['employee_name'] ); ?>
					<?php if ( ! empty( $booking['employee_email'] ) ) : ?>
						<br>
						<a href="mailto:<?php echo esc_attr( $booking['employee_email'] ); ?>" style="color: #d97706; text-decoration: none;">
							<?php echo esc_html( $booking['employee_email'] ); ?>
						</a>
					<?php endif; ?>
				</p>
			</td>
		</tr>
	</table>
<?php endif; ?>

<?php do_action( 'rtrb_email_before_booking_details', $email, $booking ); ?>

<!-- Meeting Details Box -->
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-details">
	<tr>
		<td style="padding: 20px;">
			<h3 class="booking-details-heading"><?php esc_html_e( 'Meeting Details', 'radius-booking' ); ?></h3>

			<?php if ( ! empty( $merge_tags['meeting_type_name'] ) ) : ?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
					<tr>
						<td class="detail-label"><?php esc_html_e( 'Meeting Type:', 'radius-booking' ); ?></td>
						<td align="right" class="detail-value service"><?php echo esc_html( $merge_tags['meeting_type_name'] ); ?></td>
					</tr>
				</table>
			<?php endif; ?>

			<?php if ( ! empty( $merge_tags['booking_date'] ) ) : ?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
					<tr>
						<td class="detail-label"><?php esc_html_e( 'Date:', 'radius-booking' ); ?></td>
						<td align="right" class="detail-value"><?php echo esc_html( $merge_tags['booking_date'] ); ?></td>
					</tr>
				</table>
			<?php endif; ?>

			<?php if ( ! empty( $merge_tags['time'] ) ) : ?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
					<tr>
						<td class="detail-label"><?php esc_html_e( 'Time:', 'radius-booking' ); ?></td>
						<td align="right" class="detail-value">
							<?php
							echo esc_html( $merge_tags['time'] );
							if ( ! empty( $merge_tags['end_time'] ) ) {
								echo ' – ' . esc_html( $merge_tags['end_time'] );
							}
							?>
						</td>
					</tr>
				</table>
			<?php endif; ?>

			<?php if ( ! empty( $merge_tags['duration'] ) ) : ?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
					<tr>
						<td class="detail-label"><?php esc_html_e( 'Duration:', 'radius-booking' ); ?></td>
						<td align="right" class="detail-value"><?php echo esc_html( $merge_tags['duration'] ); ?></td>
					</tr>
				</table>
			<?php endif; ?>

			<?php if ( ! empty( $merge_tags['timezone'] ) ) : ?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
					<tr>
						<td class="detail-label"><?php esc_html_e( 'Timezone:', 'radius-booking' ); ?></td>
						<td align="right" class="detail-value"><?php echo esc_html( $merge_tags['timezone'] ); ?></td>
					</tr>
				</table>
			<?php endif; ?>

			<?php
			$location_labels = array(
				'google_meet' => __( 'Google Meet', 'radius-booking' ),
				'zoom'        => __( 'Zoom', 'radius-booking' ),
				'in_person'   => __( 'In Person', 'radius-booking' ),
				'phone'       => __( 'Phone Call', 'radius-booking' ),
			);
			$location_type = $merge_tags['location_type'] ?? '';
			if ( $location_type ) :
				$location_label = $location_labels[ $location_type ] ?? ucfirst( str_replace( '_', ' ', $location_type ) );
				?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
					<tr>
						<td class="detail-label"><?php esc_html_e( 'Location:', 'radius-booking' ); ?></td>
						<td align="right" class="detail-value"><?php echo esc_html( $location_label ); ?></td>
					</tr>
				</table>
			<?php endif; ?>

			<?php if ( isset( $booking['payment']->coupon_code ) && ! empty( $booking['payment']->coupon_code ) ) : ?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
					<tr>
						<td class="detail-label"><?php esc_html_e( 'Coupon:', 'radius-booking' ); ?></td>
						<td align="right" class="detail-value"><?php echo esc_html( $booking['payment']->coupon_code ); ?></td>
					</tr>
				</table>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
					<tr>
						<td class="detail-label" style="color: #059669;"><?php esc_html_e( 'Discount:', 'radius-booking' ); ?></td>
						<td align="right" class="detail-value" style="color: #059669;">-<?php echo esc_html( get_rtrb_currency_symbol() . number_format( (float) $booking['payment']->discount_amount, 2 ) ); ?></td>
					</tr>
				</table>
			<?php endif; ?>

			<?php if ( ! empty( $merge_tags['price'] ) ) : ?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row last-row">
					<tr>
						<td class="detail-label" style="font-weight: bold; color: #111827;"><?php esc_html_e( 'Total Price:', 'radius-booking' ); ?></td>
						<td align="right" class="detail-value price"><?php echo esc_html( $merge_tags['price'] ); ?></td>
					</tr>
				</table>
			<?php endif; ?>
		</td>
	</tr>
</table>

<!-- Join Meeting Button -->
<?php if ( ! empty( $merge_tags['meeting_link'] ) && filter_var( $merge_tags['meeting_link'], FILTER_VALIDATE_URL ) ) : ?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #ecfdf5; border: 1px solid #34d399; border-radius: 6px; margin: 16px 0;">
		<tr>
			<td style="padding: 20px; text-align: center;">
				<p style="margin: 0 0 12px 0; font-weight: 600; color: #065f46; font-size: 14px;">
					<?php
					if ( ! empty( $merge_tags['meeting_provider'] ) ) {
						/* translators: %s: meeting provider name (Zoom / Google Meet). */
						printf( esc_html__( 'Join via %s', 'radius-booking' ), esc_html( ucfirst( $merge_tags['meeting_provider'] ) ) );
					} else {
						esc_html_e( 'Join Meeting', 'radius-booking' );
					}
					?>
				</p>
				<table border="0" cellspacing="0" cellpadding="0" style="margin: 0 auto;">
					<tr>
						<td style="background-color: #059669; border-radius: 6px;">
							<a href="<?php echo esc_url( $merge_tags['meeting_link'] ); ?>" style="display: inline-block; color: #ffffff; padding: 12px 28px; font-weight: 600; text-decoration: none; font-size: 14px;">
								<?php esc_html_e( 'Join Meeting', 'radius-booking' ); ?>
							</a>
						</td>
					</tr>
				</table>
				<p style="margin: 12px 0 0 0; font-size: 11px; color: #047857; word-break: break-all;">
					<?php echo esc_html( $merge_tags['meeting_link'] ); ?>
				</p>
			</td>
		</tr>
	</table>
<?php elseif ( ! empty( $merge_tags['meeting_link'] ) ) : ?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #ecfdf5; border: 1px solid #34d399; border-radius: 6px; margin: 16px 0;">
		<tr>
			<td style="padding: 16px;">
				<p style="margin: 0 0 4px 0; font-weight: 600; color: #065f46; font-size: 14px;">
					<?php esc_html_e( 'Meeting Details:', 'radius-booking' ); ?>
				</p>
				<p style="margin: 0; font-size: 14px; color: #047857;">
					<?php echo esc_html( $merge_tags['meeting_link'] ); ?>
				</p>
			</td>
		</tr>
	</table>
<?php endif; ?>

<?php do_action( 'rtrb_email_after_booking_details', $email, $booking ); ?>

<!-- Status Badge -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td align="center" style="padding: 16px 0;">
			<?php
			$booking_status = isset( $booking['status'] ) ? strtolower( $booking['status'] ) : 'pending';
			$badge_colors   = array(
				'confirmed' => array(
					'bg'    => '#d1fae5',
					'text'  => '#065f46',
					'label' => 'CONFIRMED',
				),
				'pending'   => array(
					'bg'    => '#fef3c7',
					'text'  => '#92400e',
					'label' => 'PENDING',
				),
				'cancelled' => array(
					'bg'    => '#fee2e2',
					'text'  => '#991b1b',
					'label' => 'CANCELLED',
				),
				'completed' => array(
					'bg'    => '#d1fae5',
					'text'  => '#065f46',
					'label' => 'COMPLETED',
				),
			);
			$badge = isset( $badge_colors[ $booking_status ] ) ? $badge_colors[ $booking_status ] : $badge_colors['pending'];
			?>
			<span style="background-color: <?php echo esc_attr( $badge['bg'] ); ?>; color: <?php echo esc_attr( $badge['text'] ); ?>; padding: 8px 16px; border-radius: 20px; font-size: 12px; font-weight: 600; display: inline-block;">
				<?php echo esc_html( $badge['label'] ); ?>
			</span>
		</td>
	</tr>
</table>

<!-- Notes Section -->
<?php if ( ! empty( $booking['notes'] ) ) : ?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #fef9c3; border: 1px solid #fde047; border-radius: 6px; margin: 16px 0;">
		<tr>
			<td style="padding: 16px;">
				<p style="margin: 0 0 8px 0; font-weight: 600; color: #854d0e; font-size: 14px;">
					<?php esc_html_e( 'Customer Notes:', 'radius-booking' ); ?>
				</p>
				<p style="margin: 0; font-size: 14px; color: #a16207;">
					<?php echo esc_html( $booking['notes'] ); ?>
				</p>
			</td>
		</tr>
	</table>
<?php endif; ?>

<?php if ( ! empty( $settings['footer_text'] ) ) : ?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td class="footer-text">
				<?php echo wp_kses_post( wpautop( wptexturize( $settings['footer_text'] ) ) ); ?>
			</td>
		</tr>
	</table>
<?php endif; ?>

<?php do_action( 'rtrb_email_before_button', $email, $booking ); ?>

<?php if ( ! empty( $settings['button_text'] ) ) : ?>
	<?php do_action( 'rtrb_email_button', $email, $booking, $settings ); ?>
<?php endif; ?>

<?php
do_action( 'rtrb_email_content_end', $email, $booking );
do_action( 'rtrb_email_after_content', $email, $booking );
do_action( 'rtrb_get_email_footer', $email, $booking );
