<?php
/**
 * Email Template: Admin Cancellation Notification
 *
 * Sent to admin when a booking is cancelled.
 *
 * @package KursBooking
 * @since   1.0.0
 *
 * @var string $buchung_nummer      Booking number.
 * @var string $buchung_date        Booking date.
 * @var string $customer_name       Customer name.
 * @var string $customer_email      Customer email.
 * @var string $customer_phone      Customer phone.
 * @var string $kurs_title          Course title.
 * @var string $kurs_date           Formatted course date.
 * @var string $kurs_start          Start time.
 * @var string $kurs_location       Location name.
 * @var float  $total_price         Total price.
 * @var string $cancel_date         Cancellation date.
 * @var int    $cancel_fee_percent  Cancellation fee percentage.
 * @var float  $cancel_fee_amount   Cancellation fee amount.
 * @var float  $cancel_refund_amount Refund amount.
 * @var string $cancel_reason       Cancellation reason from customer/admin.
 * @var string $cancel_fee_reason   Fee calculation reason.
 * @var string $cancel_notes        Admin notes.
 * @var string $edit_url            Admin edit URL for booking.
 * @var string $kurs_edit_url       Admin edit URL for kurs.
 */

declare(strict_types=1);

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Format function.
$format_price = function ( float $amount ): string {
	return number_format( $amount, 2, ',', '.' ) . ' €';
};

$has_refund = ( $cancel_refund_amount ?? 0 ) > 0;
?>

<div style="font-family: Arial, Helvetica, sans-serif; max-width: 600px; margin: 0 auto; background-color: #ffffff; border: 1px solid #e0e0e0; border-radius: 8px; overflow: hidden;">

	<!-- Header -->
	<div style="background-color: #dc3545; color: #ffffff; padding: 24px; text-align: center;">
		<h1 style="margin: 0; font-size: 24px; font-weight: normal;">
			⚠️ <?php esc_html_e( 'Buchung storniert', 'kurs-booking' ); ?>
		</h1>
	</div>

	<!-- Content -->
	<div style="padding: 32px;">

		<!-- Status Badge -->
		<div style="text-align: center; margin-bottom: 24px;">
			<span style="display: inline-block; background-color: #dc3545; color: #ffffff; padding: 8px 16px; border-radius: 20px; font-size: 14px; font-weight: bold;">
				<?php esc_html_e( 'Status: Storniert', 'kurs-booking' ); ?>
			</span>
		</div>

		<!-- Booking Summary -->
		<div style="background-color: #f8d7da; border: 1px solid #f5c6cb; border-radius: 6px; padding: 20px; margin-bottom: 24px;">
			<table style="width: 100%; border-collapse: collapse;">
				<tr>
					<td style="padding: 8px 0; color: #666666; width: 160px;"><?php esc_html_e( 'Buchungsnummer:', 'kurs-booking' ); ?></td>
					<td style="padding: 8px 0; color: #333333; font-weight: bold; font-size: 18px;"><?php echo esc_html( $buchung_nummer ); ?></td>
				</tr>
				<tr>
					<td style="padding: 8px 0; color: #666666;"><?php esc_html_e( 'Storniert am:', 'kurs-booking' ); ?></td>
					<td style="padding: 8px 0; color: #dc3545; font-weight: bold;"><?php echo esc_html( $cancel_date ?? '' ); ?></td>
				</tr>
			</table>
		</div>

		<!-- Course Details -->
		<div style="background-color: #e3f2fd; border: 1px solid #2196f3; border-radius: 6px; padding: 20px; margin-bottom: 24px;">
			<h3 style="font-size: 14px; text-transform: uppercase; color: #1565c0; margin: 0 0 16px 0; letter-spacing: 1px;">
				<?php esc_html_e( 'Kurs', 'kurs-booking' ); ?>
			</h3>

			<table style="width: 100%; border-collapse: collapse;">
				<tr>
					<td style="padding: 8px 0; color: #666666; width: 140px;"><?php esc_html_e( 'Kurs:', 'kurs-booking' ); ?></td>
					<td style="padding: 8px 0; color: #333333; font-weight: bold;"><?php echo esc_html( $kurs_title ); ?></td>
				</tr>
				<tr>
					<td style="padding: 8px 0; color: #666666;"><?php esc_html_e( 'Termin:', 'kurs-booking' ); ?></td>
					<td style="padding: 8px 0; color: #333333;">
						<?php echo esc_html( $kurs_date ); ?>
						<?php if ( ! empty( $kurs_start ) ) : ?>
							, <?php echo esc_html( $kurs_start ); ?> <?php esc_html_e( 'Uhr', 'kurs-booking' ); ?>
						<?php endif; ?>
					</td>
				</tr>
				<?php if ( ! empty( $kurs_location ) ) : ?>
				<tr>
					<td style="padding: 8px 0; color: #666666;"><?php esc_html_e( 'Ort:', 'kurs-booking' ); ?></td>
					<td style="padding: 8px 0; color: #333333;"><?php echo esc_html( $kurs_location ); ?></td>
				</tr>
				<?php endif; ?>
			</table>
		</div>

		<!-- Customer Details -->
		<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin-bottom: 24px;">
			<h3 style="font-size: 14px; text-transform: uppercase; color: #6c757d; margin: 0 0 16px 0; letter-spacing: 1px;">
				<?php esc_html_e( 'Kunde', 'kurs-booking' ); ?>
			</h3>

			<table style="width: 100%; border-collapse: collapse;">
				<tr>
					<td style="padding: 8px 0; color: #666666; width: 140px;"><?php esc_html_e( 'Name:', 'kurs-booking' ); ?></td>
					<td style="padding: 8px 0; color: #333333; font-weight: bold;"><?php echo esc_html( $customer_name ); ?></td>
				</tr>
				<tr>
					<td style="padding: 8px 0; color: #666666;"><?php esc_html_e( 'E-Mail:', 'kurs-booking' ); ?></td>
					<td style="padding: 8px 0;">
						<a href="mailto:<?php echo esc_attr( $customer_email ); ?>" style="color: #007bff;">
							<?php echo esc_html( $customer_email ); ?>
						</a>
					</td>
				</tr>
				<?php if ( ! empty( $customer_phone ) ) : ?>
				<tr>
					<td style="padding: 8px 0; color: #666666;"><?php esc_html_e( 'Telefon:', 'kurs-booking' ); ?></td>
					<td style="padding: 8px 0;">
						<a href="tel:<?php echo esc_attr( $customer_phone ); ?>" style="color: #007bff;">
							<?php echo esc_html( $customer_phone ); ?>
						</a>
					</td>
				</tr>
				<?php endif; ?>
			</table>
		</div>

		<!-- Financial Details -->
		<div style="background-color: #fff3cd; border: 1px solid #ffc107; border-radius: 6px; padding: 20px; margin-bottom: 24px;">
			<h3 style="font-size: 14px; text-transform: uppercase; color: #856404; margin: 0 0 16px 0; letter-spacing: 1px;">
				<?php esc_html_e( 'Finanzielle Details', 'kurs-booking' ); ?>
			</h3>

			<table style="width: 100%; border-collapse: collapse;">
				<tr>
					<td style="padding: 8px 0; color: #666666; width: 200px;"><?php esc_html_e( 'Buchungsbetrag:', 'kurs-booking' ); ?></td>
					<td style="padding: 8px 0; color: #333333; text-align: right; font-weight: bold;">
						<?php echo esc_html( $format_price( $total_price ?? 0 ) ); ?>
					</td>
				</tr>
				<tr>
					<td style="padding: 8px 0; color: #666666;">
						<?php esc_html_e( 'Stornogebühr:', 'kurs-booking' ); ?>
						<span style="font-size: 12px;">(<?php echo esc_html( (string) ( $cancel_fee_percent ?? 0 ) ); ?>%)</span>
					</td>
					<td style="padding: 8px 0; color: #28a745; text-align: right; font-weight: bold;">
						<?php echo esc_html( $format_price( $cancel_fee_amount ?? 0 ) ); ?>
					</td>
				</tr>
				<tr style="border-top: 2px solid #856404;">
					<td style="padding: 16px 0 8px 0; color: #333333; font-weight: bold;">
						<?php esc_html_e( 'Zu erstatten:', 'kurs-booking' ); ?>
					</td>
					<td style="padding: 16px 0 8px 0; color: <?php echo $has_refund ? '#dc3545' : '#6c757d'; ?>; text-align: right; font-weight: bold; font-size: 18px;">
						<?php echo esc_html( $format_price( $cancel_refund_amount ?? 0 ) ); ?>
					</td>
				</tr>
			</table>

			<!-- Fee Reason -->
			<?php if ( ! empty( $cancel_fee_reason ) ) : ?>
			<div style="margin-top: 16px; padding-top: 16px; border-top: 1px solid #ffc107;">
				<p style="margin: 0; color: #856404; font-size: 14px;">
					<strong><?php esc_html_e( 'Gebühren-Berechnung:', 'kurs-booking' ); ?></strong><br>
					<?php echo esc_html( $cancel_fee_reason ); ?>
				</p>
			</div>
			<?php endif; ?>
		</div>

		<!-- Cancellation Reason -->
		<?php if ( ! empty( $cancel_reason ) ) : ?>
		<div style="background-color: #f8f9fa; border: 1px solid #dee2e6; border-radius: 6px; padding: 20px; margin-bottom: 24px;">
			<h3 style="font-size: 14px; text-transform: uppercase; color: #6c757d; margin: 0 0 12px 0; letter-spacing: 1px;">
				<?php esc_html_e( 'Stornierungsgrund', 'kurs-booking' ); ?>
			</h3>
			<p style="margin: 0; color: #333333;">
				<?php echo esc_html( $cancel_reason ); ?>
			</p>
		</div>
		<?php endif; ?>

		<!-- Admin Notes -->
		<?php if ( ! empty( $cancel_notes ) ) : ?>
		<div style="background-color: #e8eaf6; border: 1px solid #9fa8da; border-radius: 6px; padding: 20px; margin-bottom: 24px;">
			<h3 style="font-size: 14px; text-transform: uppercase; color: #3f51b5; margin: 0 0 12px 0; letter-spacing: 1px;">
				<?php esc_html_e( 'Admin-Notizen', 'kurs-booking' ); ?>
			</h3>
			<p style="margin: 0; color: #333333; white-space: pre-wrap;">
				<?php echo esc_html( $cancel_notes ); ?>
			</p>
		</div>
		<?php endif; ?>

		<!-- Action Required -->
		<?php if ( $has_refund ) : ?>
		<div style="background-color: #ffebee; border: 2px solid #ef5350; border-radius: 6px; padding: 20px; margin-bottom: 24px;">
			<h3 style="font-size: 14px; text-transform: uppercase; color: #c62828; margin: 0 0 12px 0; letter-spacing: 1px;">
				⚡ <?php esc_html_e( 'Aktion erforderlich', 'kurs-booking' ); ?>
			</h3>
			<p style="margin: 0; color: #c62828;">
				<?php
				printf(
					/* translators: %s: Refund amount */
					esc_html__( 'Bitte veranlassen Sie die Rückerstattung von %s an den Kunden.', 'kurs-booking' ),
					'<strong>' . esc_html( $format_price( $cancel_refund_amount ?? 0 ) ) . '</strong>'
				);
				?>
			</p>
		</div>
		<?php endif; ?>

		<!-- Action Buttons -->
		<div style="text-align: center; margin-bottom: 24px;">
			<a href="<?php echo esc_url( $edit_url ); ?>"
			   style="display: inline-block; background-color: #007bff; color: #ffffff; padding: 12px 24px; text-decoration: none; border-radius: 6px; font-weight: bold; margin: 4px;">
				<?php esc_html_e( 'Buchung anzeigen', 'kurs-booking' ); ?>
			</a>
			<a href="<?php echo esc_url( $kurs_edit_url ); ?>"
			   style="display: inline-block; background-color: #6c757d; color: #ffffff; padding: 12px 24px; text-decoration: none; border-radius: 6px; font-weight: bold; margin: 4px;">
				<?php esc_html_e( 'Kurs anzeigen', 'kurs-booking' ); ?>
			</a>
		</div>

	</div>

	<!-- Footer -->
	<div style="background-color: #f8f9fa; padding: 16px 24px; border-top: 1px solid #e0e0e0; text-align: center;">
		<p style="margin: 0; font-size: 12px; color: #6c757d;">
			<?php esc_html_e( 'Diese E-Mail wurde automatisch vom Kurs-Booking System gesendet.', 'kurs-booking' ); ?>
		</p>
	</div>

</div>
