<?php
/**
 * ============================================
 * FILE: templates/emails/admin/new-event.php
 * Admin New Event Registration 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 event registration has been created in the system. Please review the details below.', '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, #faf5ff 0%, #ede9fe 100%); border: 1px solid #8b5cf6; border-radius: 6px; margin: 16px 0;">
	<tr>
		<td style="padding: 16px;">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td width="33%" style="padding: 8px; text-align: center; border-right: 1px solid #c4b5fd;">
						<p style="margin: 0 0 4px 0; font-size: 12px; color: #6d28d9; font-weight: 500;">
							<?php esc_html_e( 'Booking ID', 'radius-booking' ); ?>
						</p>
						<p style="margin: 0; font-size: 18px; color: #4c1d95; font-weight: bold;">
							#<?php echo esc_html( $booking['customer']->id ?? '' ); ?>
						</p>
					</td>
					<td width="33%" style="padding: 8px; text-align: center; border-right: 1px solid #c4b5fd;">
						<p style="margin: 0 0 4px 0; font-size: 12px; color: #6d28d9; font-weight: 500;">
							<?php esc_html_e( 'Tickets', 'radius-booking' ); ?>
						</p>
						<p style="margin: 0; font-size: 18px; color: #4c1d95; font-weight: bold;">
							<?php echo esc_html( $merge_tags['ticket_count'] ?? 0 ); ?>
						</p>
					</td>
					<td width="34%" style="padding: 8px; text-align: center;">
						<p style="margin: 0 0 4px 0; font-size: 12px; color: #6d28d9; font-weight: 500;">
							<?php esc_html_e( 'Revenue', 'radius-booking' ); ?>
						</p>
						<p style="margin: 0; font-size: 18px; color: #4c1d95; font-weight: bold;">
							<?php echo esc_html( $merge_tags['total_price'] ?? '' ); ?>
						</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( 'Registrant 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>

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

<!-- Event 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( 'Event Details', 'radius-booking' ); ?></h3>

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

			<?php if ( ! empty( $merge_tags['event_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['event_date'] );
							if ( ! empty( $merge_tags['event_end_date'] ) && $merge_tags['event_end_date'] !== $merge_tags['event_date'] ) {
								echo ' – ' . esc_html( $merge_tags['event_end_date'] );
							}
							?>
						</td>
					</tr>
				</table>
			<?php endif; ?>

			<?php if ( ! empty( $merge_tags['event_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['event_time'] );
							if ( ! empty( $merge_tags['event_end_time'] ) ) {
								echo ' – ' . esc_html( $merge_tags['event_end_time'] );
							}
							?>
						</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 if ( ! empty( $merge_tags['event_location'] ) ) : ?>
				<table width="100%" border="0" cellspacing="0" cellpadding="0" class="booking-detail-row">
					<tr>
						<td class="detail-label">
							<?php
							if ( 'online' === ( $merge_tags['event_location_type'] ?? '' ) ) {
								esc_html_e( 'Online:', 'radius-booking' );
							} else {
								esc_html_e( 'Venue:', 'radius-booking' );
							}
							?>
						</td>
						<td align="right" class="detail-value"><?php echo esc_html( $merge_tags['event_location'] ); ?></td>
					</tr>
				</table>
			<?php endif; ?>
		</td>
	</tr>
</table>

<!-- Tickets Box -->
<?php if ( ! empty( $booking['event_tickets'] ) ) : ?>
	<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #ffffff; 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( 'Tickets', 'radius-booking' ); ?>
				</p>
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
					<tr>
						<td style="padding: 6px 4px; font-size: 12px; color: #6b7280; font-weight: 600; text-transform: uppercase; border-bottom: 1px solid #e5e7eb;"><?php esc_html_e( 'Ticket', 'radius-booking' ); ?></td>
						<td align="center" style="padding: 6px 4px; font-size: 12px; color: #6b7280; font-weight: 600; text-transform: uppercase; border-bottom: 1px solid #e5e7eb;"><?php esc_html_e( 'Qty', 'radius-booking' ); ?></td>
						<td align="right" style="padding: 6px 4px; font-size: 12px; color: #6b7280; font-weight: 600; text-transform: uppercase; border-bottom: 1px solid #e5e7eb;"><?php esc_html_e( 'Price', 'radius-booking' ); ?></td>
						<td align="right" style="padding: 6px 4px; font-size: 12px; color: #6b7280; font-weight: 600; text-transform: uppercase; border-bottom: 1px solid #e5e7eb;"><?php esc_html_e( 'Subtotal', 'radius-booking' ); ?></td>
					</tr>
					<?php foreach ( $booking['event_tickets'] as $ticket ) : ?>
						<?php
						$qty      = (int) ( $ticket['quantity'] ?? 1 );
						$price    = (float) ( $ticket['price'] ?? 0 );
						$subtotal = $qty * $price;
						?>
						<tr>
							<td style="padding: 8px 4px; font-size: 14px; color: #111827; border-bottom: 1px solid #f3f4f6;">
								<?php echo esc_html( $ticket['name'] ?? '' ); ?>
								<?php if ( ! empty( $ticket['registration_number'] ) ) : ?>
									<br><span style="font-size: 11px; color: #9ca3af;">#<?php echo esc_html( $ticket['registration_number'] ); ?></span>
								<?php endif; ?>
							</td>
							<td align="center" style="padding: 8px 4px; font-size: 14px; color: #374151; border-bottom: 1px solid #f3f4f6;">
								<?php echo esc_html( $qty ); ?>
							</td>
							<td align="right" style="padding: 8px 4px; font-size: 14px; color: #374151; border-bottom: 1px solid #f3f4f6;">
								<?php echo esc_html( get_rtrb_currency_symbol() . number_format( $price, 2 ) ); ?>
							</td>
							<td align="right" style="padding: 8px 4px; font-size: 14px; color: #111827; font-weight: 600; border-bottom: 1px solid #f3f4f6;">
								<?php echo esc_html( get_rtrb_currency_symbol() . number_format( $subtotal, 2 ) ); ?>
							</td>
						</tr>
					<?php endforeach; ?>
				</table>

				<?php if ( isset( $booking['payment']->coupon_code ) && ! empty( $booking['payment']->coupon_code ) ) : ?>
					<table width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-top: 8px;">
						<tr>
							<td style="padding: 4px; font-size: 13px; color: #4b5563;"><?php esc_html_e( 'Coupon:', 'radius-booking' ); ?> <strong><?php echo esc_html( $booking['payment']->coupon_code ); ?></strong></td>
							<td align="right" style="padding: 4px; font-size: 13px; 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 ( isset( $booking['payment']->tax_amount ) && $booking['payment']->tax_amount > 0 ) : ?>
					<?php
					$tax_settings = \RadiusTheme\RadiusBooking\Helpers\SettingsHelper::get_setting( 'tax' );
					$tax_label    = ! empty( $tax_settings['label'] ) ? $tax_settings['label'] : __( 'Tax', 'radius-booking' );
					?>
					<table width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-top: 4px;">
						<tr>
							<td style="padding: 4px; font-size: 13px; color: #4b5563;">
								<?php echo esc_html( $tax_label ); ?> (<?php echo esc_html( $booking['payment']->tax_rate ); ?>%)
							</td>
							<td align="right" style="padding: 4px; font-size: 13px; color: #374151;">+<?php echo esc_html( get_rtrb_currency_symbol() . number_format( (float) $booking['payment']->tax_amount, 2 ) ); ?></td>
						</tr>
					</table>
				<?php endif; ?>

				<table width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-top: 8px; border-top: 2px solid #e5e7eb;">
					<tr>
						<td style="padding: 10px 4px 4px; font-size: 15px; color: #111827; font-weight: bold;">
							<?php esc_html_e( 'Total:', 'radius-booking' ); ?>
						</td>
						<td align="right" style="padding: 10px 4px 4px; font-size: 18px; color: #7c3aed; font-weight: bold;">
							<?php echo esc_html( $merge_tags['total_price'] ?? '' ); ?>
						</td>
					</tr>
				</table>
			</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( 'Registrant 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 );
