<?php
/**
 * Metrics dashboard view.
 *
 * @var array<string, array<string, mixed>> $data
 *
 * @package MailDoctor
 */

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

$mail_doctor_ranges   = $data['ranges'] ?? array();
$mail_doctor_timeline = $data['timeline'] ?? array();
?>
<?php $mail_doctor_main_id = 'mail-doctor-metrics'; ?>
<div class="wrap mail-doctor">
	<a class="mail-doctor__skip-link" href="#<?php echo esc_attr( $mail_doctor_main_id ); ?>"><?php esc_html_e( 'Skip to Mail Doctor content', 'mail-doctor' ); ?></a>
	<h1><?php esc_html_e( 'Mail Doctor Metrics', 'mail-doctor' ); ?></h1>

	<p><?php esc_html_e( 'Rolling view of send volume, latency, and failures to help monitor email health.', 'mail-doctor' ); ?></p>

	<main id="<?php echo esc_attr( $mail_doctor_main_id ); ?>" class="mail-doctor__main" tabindex="-1">
	<div class="mail-doctor__metric-cards" role="region" aria-label="<?php esc_attr_e( 'Summary metrics', 'mail-doctor' ); ?>">
		<?php
		foreach ( $mail_doctor_ranges as $mail_doctor_label => $mail_doctor_metrics ) :
			$mail_doctor_sent    = (int) ( $mail_doctor_metrics['sent'] ?? 0 );
			$mail_doctor_failed  = (int) ( $mail_doctor_metrics['failed'] ?? 0 );
			$mail_doctor_total   = (int) ( $mail_doctor_metrics['total'] ?? 0 );
			$mail_doctor_success = isset( $mail_doctor_metrics['success_rate'] ) ? (float) $mail_doctor_metrics['success_rate'] : 0.0;
			$mail_doctor_median  = isset( $mail_doctor_metrics['median_latency'] ) ? (float) $mail_doctor_metrics['median_latency'] : 0.0;
			$mail_doctor_avg     = isset( $mail_doctor_metrics['avg_latency'] ) ? (float) $mail_doctor_metrics['avg_latency'] : 0.0;
			?>
			<article class="mail-doctor__metric-card">
				<h2><?php echo esc_html( strtoupper( $mail_doctor_label ) ); ?></h2>
				<ul>
					<li><strong><?php esc_html_e( 'Total sends', 'mail-doctor' ); ?>:</strong> <?php echo esc_html( number_format_i18n( $mail_doctor_total ) ); ?></li>
					<li><strong><?php esc_html_e( 'Delivered', 'mail-doctor' ); ?>:</strong> <?php echo esc_html( number_format_i18n( $mail_doctor_sent ) ); ?></li>
					<li><strong><?php esc_html_e( 'Failed', 'mail-doctor' ); ?>:</strong> <?php echo esc_html( number_format_i18n( $mail_doctor_failed ) ); ?></li>
					<li><strong><?php esc_html_e( 'Success rate', 'mail-doctor' ); ?>:</strong> <?php echo esc_html( $mail_doctor_success ); ?>%</li>
					<li><strong><?php esc_html_e( 'Median latency', 'mail-doctor' ); ?>:</strong> <?php echo esc_html( number_format_i18n( $mail_doctor_median ) ); ?> ms</li>
					<li><strong><?php esc_html_e( 'Average latency', 'mail-doctor' ); ?>:</strong> <?php echo esc_html( number_format_i18n( $mail_doctor_avg ) ); ?> ms</li>
				</ul>

				<?php if ( ! empty( $mail_doctor_metrics['failed_by_code'] ) ) : ?>
					<h3><?php esc_html_e( 'Top failure codes', 'mail-doctor' ); ?></h3>
					<ul>
						<?php foreach ( $mail_doctor_metrics['failed_by_code'] as $mail_doctor_row ) : ?>
							<li><?php echo esc_html( sprintf( '%s — %s', $mail_doctor_row['provider_code'], number_format_i18n( $mail_doctor_row['total'] ) ) ); ?></li>
						<?php endforeach; ?>
					</ul>
				<?php endif; ?>

				<?php if ( ! empty( $mail_doctor_metrics['failed_by_event'] ) ) : ?>
					<h3><?php esc_html_e( 'Top events', 'mail-doctor' ); ?></h3>
					<ul>
						<?php foreach ( $mail_doctor_metrics['failed_by_event'] as $mail_doctor_row ) : ?>
							<li><?php echo esc_html( sprintf( '%s — %s', $mail_doctor_row['event'], number_format_i18n( $mail_doctor_row['total'] ) ) ); ?></li>
						<?php endforeach; ?>
					</ul>
				<?php endif; ?>
			</article>
		<?php endforeach; ?>
	</div>

	<?php if ( empty( $mail_doctor_ranges ) ) : ?>
		<p><?php esc_html_e( 'No data available yet. Send some test emails to populate metrics.', 'mail-doctor' ); ?></p>
	<?php endif; ?>

	<section class="mail-doctor__panel mail-doctor__timeline" role="region" aria-labelledby="mail-doctor-timeline">
		<h2 id="mail-doctor-timeline"><?php esc_html_e( 'Retry Queue Timeline', 'mail-doctor' ); ?></h2>
		<p><?php esc_html_e( 'Upcoming retries, backoff history, and quarantined messages requiring attention.', 'mail-doctor' ); ?></p>

		<?php if ( empty( $mail_doctor_timeline ) ) : ?>
			<p><?php esc_html_e( 'All clear — no queued retries at the moment.', 'mail-doctor' ); ?></p>
		<?php else : ?>
			<table class="widefat fixed striped">
				<thead>
					<tr>
						<th scope="col"><?php esc_html_e( 'Log', 'mail-doctor' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Attempts', 'mail-doctor' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Next Attempt', 'mail-doctor' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Status', 'mail-doctor' ); ?></th>
						<th scope="col"><?php esc_html_e( 'SMTP Profile', 'mail-doctor' ); ?></th>
						<th scope="col"><?php esc_html_e( 'History', 'mail-doctor' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php
					foreach ( $mail_doctor_timeline as $mail_doctor_row ) :
						$mail_doctor_status_class = 'queued';
						if ( $mail_doctor_row['quarantined'] ) {
							$mail_doctor_status_class = 'failed';
						} elseif ( 'failed' === $mail_doctor_row['status'] ) {
							$mail_doctor_status_class = 'failed';
						}
						?>
						<tr>
							<td>
								<strong><?php echo esc_html( sprintf( '#%d', $mail_doctor_row['id'] ) ); ?></strong><br />
								<?php if ( ! empty( $mail_doctor_row['subject'] ) ) : ?>
									<span><?php echo esc_html( $mail_doctor_row['subject'] ); ?></span><br />
								<?php endif; ?>
								<?php if ( ! empty( $mail_doctor_row['to_email'] ) ) : ?>
									<span class="description"><?php echo esc_html( $mail_doctor_row['to_email'] ); ?></span><br />
								<?php endif; ?>
								<span class="description"><?php echo esc_html( $mail_doctor_row['created_at_local'] ); ?></span>
							</td>
							<td><?php echo esc_html( (string) $mail_doctor_row['attempts'] ); ?></td>
							<td>
								<?php if ( $mail_doctor_row['next_attempt_local'] ) : ?>
									<strong><?php echo esc_html( $mail_doctor_row['next_attempt_local'] ); ?></strong><br />
									<span class="description"><?php echo esc_html( $mail_doctor_row['next_attempt_relative'] ); ?></span>
								<?php else : ?>
									<span class="description"><?php echo esc_html( $mail_doctor_row['next_attempt_relative'] ); ?></span>
								<?php endif; ?>
							</td>
							<td>
								<span class="mail-doctor__status-badge mail-doctor__status-badge--<?php echo esc_attr( $mail_doctor_status_class ); ?>">
									<?php
									if ( $mail_doctor_row['quarantined'] ) {
										esc_html_e( 'Quarantined', 'mail-doctor' );
									} else {
										echo esc_html( ucfirst( $mail_doctor_row['status'] ) );
									}
									?>
								</span>
								<?php if ( $mail_doctor_row['quarantined'] && ! empty( $mail_doctor_row['quarantine_reason'] ) ) : ?>
									<p class="description"><?php echo esc_html( $mail_doctor_row['quarantine_reason'] ); ?></p>
								<?php endif; ?>
								<?php if ( $mail_doctor_row['quarantined'] && ! empty( $mail_doctor_row['quarantine_at_local'] ) ) : ?>
									<p class="description">
										<?php
										printf(
											/* translators: %s: date/time string */
											esc_html__( 'Since %s', 'mail-doctor' ),
											esc_html( $mail_doctor_row['quarantine_at_local'] )
										);
										?>
									</p>
							<?php endif; ?>
						</td>
						<td>
							<?php if ( ! empty( $mail_doctor_row['smtp_profile_label'] ) ) : ?>
								<?php echo esc_html( $mail_doctor_row['smtp_profile_label'] ); ?>
							<?php else : ?>
								<span class="description"><?php esc_html_e( 'Default SMTP Settings', 'mail-doctor' ); ?></span>
							<?php endif; ?>
							<?php if ( ! empty( $mail_doctor_row['smtp_profile'] ) ) : ?>
								<br /><span class="description"><?php echo esc_html( $mail_doctor_row['smtp_profile'] ); ?></span>
							<?php endif; ?>
						</td>
						<td>
							<?php if ( empty( $mail_doctor_row['history'] ) ) : ?>
								<span class="description"><?php esc_html_e( 'No recorded history yet.', 'mail-doctor' ); ?></span>
							<?php else : ?>
									<ul class="mail-doctor__timeline-history">
										<?php
										foreach ( $mail_doctor_row['history'] as $mail_doctor_entry ) :
											$mail_doctor_label_parts = array();
											if ( isset( $mail_doctor_entry['attempt'] ) ) {
												$mail_doctor_label_parts[] = sprintf(
													/* translators: %d: attempt number */
													__( 'Attempt %d', 'mail-doctor' ),
													(int) $mail_doctor_entry['attempt']
												);
											}
											if ( ! empty( $mail_doctor_entry['status'] ) ) {
												$mail_doctor_label_parts[] = ucfirst( $mail_doctor_entry['status'] );
											}
											if ( ! empty( $mail_doctor_entry['provider_code'] ) ) {
												$mail_doctor_label_parts[] = sprintf(
													/* translators: %s: provider code */
													__( 'Code %s', 'mail-doctor' ),
													$mail_doctor_entry['provider_code']
												);
											}
											?>
											<li>
												<strong><?php echo esc_html( implode( ' · ', $mail_doctor_label_parts ) ); ?></strong>
												<?php if ( ! empty( $mail_doctor_entry['occurred_at_local'] ) ) : ?>
													<br /><span class="description"><?php echo esc_html( $mail_doctor_entry['occurred_at_local'] ); ?></span>
												<?php endif; ?>
												<?php if ( ! empty( $mail_doctor_entry['error'] ) ) : ?>
													<br /><span class="description"><?php echo esc_html( $mail_doctor_entry['error'] ); ?></span>
												<?php endif; ?>
												<?php if ( ! empty( $mail_doctor_entry['delay_human'] ) ) : ?>
													<br /><span class="description">
														<?php
														printf(
															/* translators: %s: duration string */
															esc_html__( 'Backoff %s', 'mail-doctor' ),
															esc_html( $mail_doctor_entry['delay_human'] )
														);
														?>
													</span>
												<?php endif; ?>
												<?php if ( ! empty( $mail_doctor_entry['next_attempt_local'] ) ) : ?>
													<br /><span class="description">
														<?php
														printf(
															/* translators: %s: datetime string */
															esc_html__( 'Next: %s', 'mail-doctor' ),
															esc_html( $mail_doctor_entry['next_attempt_local'] )
														);
														?>
													</span>
												<?php endif; ?>
												<?php if ( ! empty( $mail_doctor_entry['reason'] ) ) : ?>
													<br /><span class="description">
														<?php
														printf(
															/* translators: %s: reason text */
															esc_html__( 'Reason: %s', 'mail-doctor' ),
															esc_html( $mail_doctor_entry['reason'] )
														);
														?>
													</span>
												<?php endif; ?>
											</li>
										<?php endforeach; ?>
									</ul>
								<?php endif; ?>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php endif; ?>
	</section>
	</main>
</div>
