<?php
/**
 * Log details view page.
 *
 * @var array<string,mixed> $mail_doctor_log_item
 * @var string              $mail_doctor_base_url
 * @var string              $mail_doctor_active_tab
 *
 * @package MailDoctor
 */

use MailDoctor\Admin;

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

$mail_doctor_item  = $mail_doctor_log_item;
$mail_doctor_modal = $mail_doctor_item['modal'] ?? array();
$mail_doctor_id    = absint( $mail_doctor_item['id'] ?? 0 );
$mail_doctor_back_url = Admin::page_url( 'logs' );

$mail_doctor_eml_url = '';
if ( $mail_doctor_id && \MailDoctor\Features::is_enabled( 'eml_download' ) ) {
	$mail_doctor_eml_url = wp_nonce_url(
		add_query_arg(
			array(
				'action' => 'mail_doctor_export_eml',
				'log_id' => $mail_doctor_id,
			),
			admin_url( 'admin-post.php' )
		),
		'mail_doctor_export_eml_' . $mail_doctor_id
	);
}
?>

<?php if ( empty( $mail_doctor_is_drawer ) ) : ?>
<div class="wrap maildoctor-admin-wrap mail-doctor-log-view-page">
	<?php settings_errors( 'maildoctor' ); ?>

	<div class="mail-doctor-log-view-header" style="margin-bottom: 20px;">
		<a href="<?php echo esc_url( $mail_doctor_back_url ); ?>" class="button button-secondary mail-doctor-back-btn">
			&larr; <?php esc_html_e( 'Back to Logs', 'mail-doctor' ); ?>
		</a>
	</div>
<?php endif; ?>

	<div class="mail-doctor-log-view-columns">
		<div class="mail-doctor-log-view-main">
			<div class="mail-doctor-log-overview">
				<div class="mail-doctor-log-overview__header">
					<h3 class="mail-doctor-log-subject"><?php echo esc_html( $mail_doctor_modal['subject'] ?? '' ); ?></h3>
					<span class="mail-doctor-log-status" data-value="<?php echo esc_attr( $mail_doctor_item['status'] ?? '' ); ?>">
						<?php echo esc_html( $mail_doctor_modal['status'] ?? '' ); ?>
					</span>
				</div>
				<div class="mail-doctor-log-overview__grid">
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Date', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_item['time_display'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Log ID', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value">#<?php echo esc_html( $mail_doctor_id ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Recipient', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['email'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'From', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['from'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Event', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['event'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Status', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( ucfirst( $mail_doctor_item['status'] ?? '' ) ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Attachments', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value">
							<?php
							if ( $mail_doctor_item['attachments'] > 0 ) {
								echo esc_html(
									sprintf(
										/* translators: %d: number of attachments. */
										_n( '%d attachment', '%d attachments', $mail_doctor_item['attachments'], 'mail-doctor' ),
										$mail_doctor_item['attachments']
									)
								);
							} else {
								esc_html_e( 'None', 'mail-doctor' );
							}
							?>
						</span>
					</div>
				</div>
			</div>

			<div class="mail-doctor-log-technical" style="margin-top: 20px;">
				<h4 class="mail-doctor-log-section-title"><?php esc_html_e( 'Technical Details', 'mail-doctor' ); ?></h4>
				<div class="mail-doctor-log-tech-grid">
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Delivery method', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['transport'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Mailer Name', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_item['provider'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Provider response', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['provider'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Profile', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['profile'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Latency', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['latency'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Environment', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['environment'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Message ID', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['messageId'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Content hash', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['contentHash'] ?? '' ); ?></span>
					</div>
				</div>
				
				<div class="mail-doctor-log-code-blocks" style="margin-top: 20px;">
					<div class="mail-doctor-log-code-item">
						<h5 class="mail-doctor-log-code-title"><?php esc_html_e( 'Attempt history', 'mail-doctor' ); ?></h5>
						<pre><?php echo esc_html( $mail_doctor_modal['history'] ?? '' ); ?></pre>
					</div>
					<div class="mail-doctor-log-code-item">
						<h5 class="mail-doctor-log-code-title"><?php esc_html_e( 'Headers', 'mail-doctor' ); ?></h5>
						<pre><?php echo esc_html( $mail_doctor_modal['headers'] ?? '' ); ?></pre>
					</div>
					<div class="mail-doctor-log-code-item">
						<h5 class="mail-doctor-log-code-title"><?php esc_html_e( 'Transport debug', 'mail-doctor' ); ?></h5>
						<pre><?php echo esc_html( $mail_doctor_modal['transportDebug'] ?? '' ); ?></pre>
					</div>
					<div class="mail-doctor-log-code-item">
						<h5 class="mail-doctor-log-code-title"><?php esc_html_e( 'Body preview', 'mail-doctor' ); ?></h5>
						<pre><?php echo esc_html( $mail_doctor_modal['bodyPreview'] ?? '' ); ?></pre>
					</div>
				</div>
			</div>
		</div>

		<div class="mail-doctor-log-view-sidebar">
			<div class="mail-doctor-log-sidebar-card">
				<h4 class="mail-doctor-log-section-title" style="border-bottom: 1px solid var(--mail-doctor-color-border); padding-bottom: 10px; margin-bottom: 15px;"><?php esc_html_e( 'Actions', 'mail-doctor' ); ?></h4>
				<ul class="mail-doctor-log-actions-list">
					<li>
						<a href="#" class="mail-doctor-action-link" onclick="window.print(); return false;">
							<span class="dashicons dashicons-printer"></span>
							<?php esc_html_e( 'Print', 'mail-doctor' ); ?>
						</a>
					</li>
					<?php if ( $mail_doctor_eml_url ) : ?>
					<li>
						<a href="<?php echo esc_url( $mail_doctor_eml_url ); ?>" class="mail-doctor-action-link" download>
							<span class="dashicons dashicons-download"></span>
							<?php esc_html_e( 'Export (EML)', 'mail-doctor' ); ?>
						</a>
					</li>
					<?php endif; ?>
					<?php if ( 'failed' === $mail_doctor_item['status'] ) : ?>
					<li>
						<a href="#" class="mail-doctor-action-link mail-doctor-retry-log" data-log-id="<?php echo esc_attr( $mail_doctor_id ); ?>">
							<span class="dashicons dashicons-update"></span>
							<?php esc_html_e( 'Retry Delivery', 'mail-doctor' ); ?>
						</a>
					</li>
					<?php endif; ?>
					<li>
						<a href="#" class="mail-doctor-action-link mail-doctor-resend-log" data-log-id="<?php echo esc_attr( $mail_doctor_id ); ?>">
							<span class="dashicons dashicons-controls-forward"></span>
							<?php esc_html_e( 'Resend to...', 'mail-doctor' ); ?>
						</a>
					</li>
					<li>
						<a href="#" class="mail-doctor-action-link mail-doctor-delete-log mail-doctor-color-error" data-log-id="<?php echo esc_attr( $mail_doctor_id ); ?>">
							<span class="dashicons dashicons-trash"></span>
							<?php esc_html_e( 'Delete Log', 'mail-doctor' ); ?>
						</a>
					</li>
				</ul>
			</div>

			<div class="mail-doctor-log-sidebar-card" style="margin-top: 20px;">
				<h4 class="mail-doctor-log-section-title"><?php esc_html_e( 'Log Details', 'mail-doctor' ); ?></h4>
				<div class="mail-doctor-log-tech-grid" style="grid-template-columns: 1fr;">
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Log ID', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value">#<?php echo esc_html( $mail_doctor_modal['logId'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Status', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['status'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Mailer', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['mailerName'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Attachments', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['attachmentCount'] ?? '0' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Attempts', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['attempts'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Next retry', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['nextRetry'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Reply-to', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['replyTo'] ?? '' ); ?></span>
					</div>
					<div class="mail-doctor-log-detail-item">
						<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Return path', 'mail-doctor' ); ?></span>
						<span class="mail-doctor-log-detail-value"><?php echo esc_html( $mail_doctor_modal['returnPath'] ?? '' ); ?></span>
					</div>
					
					<?php if ( ! empty( $mail_doctor_modal['error'] ) && __( 'N/A', 'mail-doctor' ) !== $mail_doctor_modal['error'] ) : ?>
						<div class="mail-doctor-log-detail-item">
							<span class="mail-doctor-log-detail-label"><?php esc_html_e( 'Last error', 'mail-doctor' ); ?></span>
							<span class="mail-doctor-log-detail-value mail-doctor-error-text"><?php echo esc_html( $mail_doctor_modal['error'] ); ?></span>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>
<?php if ( empty( $mail_doctor_is_drawer ) ) : ?>
</div>
<?php endif; ?>
