<?php
/**
 * Mail Doctor status dashboard scaffold.
 *
 * @var string $active_tab
 * @var string $base_url
 * @var string $test_email
 * @var array<string,string> $provider_options
 *
 * @package MailDoctor
 */

use MailDoctor\Admin;

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

?>
<div class="wrap maildoctor-admin-wrap">

	<?php settings_errors( 'maildoctor' ); ?>

	<div class="mail-doctor-status-layers">
		<section class="mail-doctor-status-primary">
			<div class="mail-doctor-kpi-grid">
			<div class="mail-doctor-card mail-doctor-card--chart" id="mail-doctor-status-stats" aria-busy="true">
				<div class="mail-doctor-card__header">
					<h3><?php esc_html_e( 'Delivery (24h)', 'mail-doctor' ); ?></h3>
					<a href="<?php echo esc_url( Admin::page_url( 'logs' ) ); ?>" class="mail-doctor-card__action-link"><?php esc_html_e( 'View Logs', 'mail-doctor' ); ?></a>
				</div>
				<div class="mail-doctor-chart-grid">
					<div class="mail-doctor-chart-metrics-col">
						<div class="mail-doctor-kpi-block">
							<span class="mail-doctor-kpi-label"><?php esc_html_e( 'Success rate', 'mail-doctor' ); ?></span>
							<div class="mail-doctor-kpi-rate-row">
								<span class="mail-doctor-kpi-value" data-field="successRate">—</span>
								<div class="mail-doctor-trend-badge" id="mail-doctor-delivery-trend">
									<span class="mail-doctor-trend__delta" data-field="trendDelta" data-direction="flat">—</span>
								</div>
							</div>
						</div>
						
						<div class="mail-doctor-kpi-meta-row">
							<div class="mail-doctor-kpi-meta-item">
								<span class="mail-doctor-kpi-meta-label"><?php esc_html_e( 'Total emails', 'mail-doctor' ); ?></span>
								<strong class="mail-doctor-kpi-meta-value" data-field="totalCount">—</strong>
							</div>
							<div class="mail-doctor-kpi-meta-item">
								<span class="mail-doctor-kpi-meta-label"><?php esc_html_e( 'Median latency', 'mail-doctor' ); ?></span>
								<strong class="mail-doctor-kpi-meta-value" data-field="medianLatency">—</strong>
							</div>
						</div>

						<div class="mail-doctor-trend-sparkline-wrap">
							<svg viewBox="0 0 160 60" preserveAspectRatio="none" data-field="trendChart" aria-hidden="true"></svg>
							<p class="mail-doctor-trend__caption" data-field="trendCaption">&nbsp;</p>
						</div>
					</div>
					
					<div class="mail-doctor-chart-visual-col">
						<div class="mail-doctor-chart__canvas-wrap">
							<canvas id="mail-doctor-delivery-chart" width="130" height="130" aria-hidden="true"></canvas>
							<div class="mail-doctor-chart__center" data-field="deliveryCenter">—</div>
						</div>
						
						<ul class="mail-doctor-chart-legend mail-doctor-chart-legend--stacked">
							<li>
								<span class="mail-doctor-chart-dot is-sent" aria-hidden="true"></span>
								<span class="mail-doctor-chart-label"><?php esc_html_e( 'Sent', 'mail-doctor' ); ?></span>
								<strong data-field="sentCount">—</strong>
							</li>
							<li>
								<span class="mail-doctor-chart-dot is-failed" aria-hidden="true"></span>
								<span class="mail-doctor-chart-label"><?php esc_html_e( 'Failed', 'mail-doctor' ); ?></span>
								<strong data-field="failedCount">—</strong>
							</li>
						</ul>
					</div>
				</div>
				<p class="mail-doctor-chart-footnote" data-field="deliveryNarrative" aria-live="polite">&nbsp;</p>
				<span class="spinner is-active"></span>
			</div>

			<div class="mail-doctor-card mail-doctor-card--chart" id="mail-doctor-status-queue" aria-busy="true">
				<div class="mail-doctor-card__header">
					<h3><?php esc_html_e( 'Queue Snapshot', 'mail-doctor' ); ?></h3>
					<p class="mail-doctor-card__meta" data-field="next">&nbsp;</p>
				</div>
				<div class="mail-doctor-queue-stats-col">
					<div class="mail-doctor-queue-stat-item">
						<span class="mail-doctor-queue-stat-label"><?php esc_html_e( 'Queued', 'mail-doctor' ); ?></span>
						<strong class="mail-doctor-queue-stat-value" data-field="queueQueued">—</strong>
					</div>
					<div class="mail-doctor-queue-stat-item is-failed-block">
						<span class="mail-doctor-queue-stat-label"><?php esc_html_e( 'Failed', 'mail-doctor' ); ?></span>
						<strong class="mail-doctor-queue-stat-value" data-field="queueFailed">—</strong>
					</div>
					<div class="mail-doctor-queue-stat-item">
						<span class="mail-doctor-queue-stat-label"><?php esc_html_e( 'Quarantined', 'mail-doctor' ); ?></span>
						<strong class="mail-doctor-queue-stat-value" data-field="queueQuarantined">—</strong>
					</div>
				</div>
				<p class="mail-doctor-chart-footnote" data-field="queueNarrative" aria-live="polite">&nbsp;</p>
				<span class="spinner is-active"></span>
			</div>
			</div>

			<div class="mail-doctor-info-grid">
				<div class="mail-doctor-card mail-doctor-card--inline" id="mail-doctor-status-dns" aria-busy="true">
					<div class="mail-doctor-card__header">
						<h3><?php esc_html_e( 'DNS Posture', 'mail-doctor' ); ?></h3>
					</div>
					<ul class="mail-doctor-badge-list">
						<li data-signal="spf"><span class="mail-doctor-badge" title=""><span
								class="screen-reader-text"><?php esc_html_e( 'SPF status', 'mail-doctor' ); ?></span><?php esc_html_e( 'SPF', 'mail-doctor' ); ?></span>
						</li>
						<li data-signal="dkim"><span class="mail-doctor-badge" title=""><span
								class="screen-reader-text"><?php esc_html_e( 'DKIM status', 'mail-doctor' ); ?></span><?php esc_html_e( 'DKIM', 'mail-doctor' ); ?></span>
						</li>
						<li data-signal="dmarc"><span class="mail-doctor-badge" title=""><span
								class="screen-reader-text"><?php esc_html_e( 'DMARC status', 'mail-doctor' ); ?></span><?php esc_html_e( 'DMARC', 'mail-doctor' ); ?></span>
						</li>
						<li data-signal="bimi"><span class="mail-doctor-badge" title=""><span
								class="screen-reader-text"><?php esc_html_e( 'BIMI status', 'mail-doctor' ); ?></span><?php esc_html_e( 'BIMI', 'mail-doctor' ); ?></span>
						</li>
						<li data-signal="mta_sts"><span class="mail-doctor-badge" title=""><span
								class="screen-reader-text"><?php esc_html_e( 'MTA-STS status', 'mail-doctor' ); ?></span><?php esc_html_e( 'MTA-STS', 'mail-doctor' ); ?></span>
						</li>
						<li data-signal="tls_rpt"><span class="mail-doctor-badge" title=""><span
								class="screen-reader-text"><?php esc_html_e( 'TLS-RPT status', 'mail-doctor' ); ?></span><?php esc_html_e( 'TLS-RPT', 'mail-doctor' ); ?></span>
						</li>
					</ul>
					<span class="spinner is-active"></span>
				</div>

				<div class="mail-doctor-card" id="mail-doctor-status-warnings" aria-busy="true">
					<h3><?php esc_html_e( 'Warnings', 'mail-doctor' ); ?></h3>
					<ul class="mail-doctor-warning-list"></ul>
					<span class="spinner is-active"></span>
				</div>

				<div class="mail-doctor-card mail-doctor-card--stacked" id="mail-doctor-status-queue-health" aria-busy="true">
					<h3><?php esc_html_e( 'Queue Health', 'mail-doctor' ); ?></h3>
					<div class="mail-doctor-health-grid">
						<div class="mail-doctor-health-tile">
							<span class="mail-doctor-health-tile__label"><?php esc_html_e( 'Average attempts', 'mail-doctor' ); ?></span>
							<strong class="mail-doctor-health-tile__value" data-field="queueAvgAttempts">—</strong>
						</div>
						<div class="mail-doctor-health-tile">
							<span class="mail-doctor-health-tile__label"><?php esc_html_e( 'Queued backlog', 'mail-doctor' ); ?></span>
							<strong class="mail-doctor-health-tile__value" data-field="queueQueuedCount">—</strong>
							<span class="mail-doctor-health-tile__note" data-field="queueQueuedAge">&nbsp;</span>
						</div>
						<div class="mail-doctor-health-tile">
							<span class="mail-doctor-health-tile__label"><?php esc_html_e( 'Quarantined', 'mail-doctor' ); ?></span>
							<strong class="mail-doctor-health-tile__value" data-field="queueQuarantineCount">—</strong>
							<span class="mail-doctor-health-tile__note" data-field="queueQuarantineAge">&nbsp;</span>
						</div>
						<div class="mail-doctor-health-tile">
							<span class="mail-doctor-health-tile__label"><?php esc_html_e( 'Next retry', 'mail-doctor' ); ?></span>
							<strong class="mail-doctor-health-tile__value" data-field="queueNextRetry">—</strong>
						</div>
					</div>
					<span class="spinner is-active"></span>
				</div>

				<div class="mail-doctor-card mail-doctor-card--stacked" id="mail-doctor-status-failure-mix" aria-busy="true">
					<h3><?php esc_html_e( 'Failure Mix (24h)', 'mail-doctor' ); ?></h3>
					<div class="mail-doctor-failure-bar" role="img" aria-live="polite">
						<div class="mail-doctor-failure-bar__segments" data-field="failureMixBar"></div>
					</div>
					<ul class="mail-doctor-failure-legend" data-field="failureMixLegend"></ul>
					<p class="mail-doctor-empty" data-field="failureMixEmpty">&nbsp;</p>
					<span class="spinner is-active"></span>
				</div>

				<div class="mail-doctor-card mail-doctor-card--stacked" id="mail-doctor-status-failures" aria-busy="true">
					<h3><?php esc_html_e( 'Failure Spotlight', 'mail-doctor' ); ?></h3>
					<ul class="mail-doctor-failure-list"></ul>
					<p class="mail-doctor-empty" data-field="failuresEmpty">&nbsp;</p>
					<span class="spinner is-active"></span>
				</div>

				<div class="mail-doctor-card mail-doctor-card--stacked" id="mail-doctor-status-providers" aria-busy="true">
					<h3><?php esc_html_e( 'Provider Scoreboard', 'mail-doctor' ); ?></h3>
					<table class="mail-doctor-provider-table" role="presentation">
						<thead>
							<tr>
								<th scope="col"><?php esc_html_e( 'Provider', 'mail-doctor' ); ?></th>
								<th scope="col"><?php esc_html_e( 'Success', 'mail-doctor' ); ?></th>
								<th scope="col"><?php esc_html_e( 'Latency', 'mail-doctor' ); ?></th>
								<th scope="col"><?php esc_html_e( 'Volume', 'mail-doctor' ); ?></th>
							</tr>
						</thead>
						<tbody class="mail-doctor-provider-table__body"></tbody>
					</table>
					<p class="mail-doctor-empty" data-field="providersEmpty">&nbsp;</p>
					<span class="spinner is-active"></span>
				</div>
			</div>
		</section>

		<aside class="mail-doctor-status-sidebar">
			<div class="mail-doctor-card mail-doctor-card--stacked">
				<h3><?php esc_html_e( 'Quick Actions', 'mail-doctor' ); ?></h3>
				<?php $mail_doctor_quick_settings = Admin::get_settings(); ?>
				<form id="mail-doctor-test-email-form" action="#" method="post">
					<p class="mail-doctor-input-group">
						<label for="mail-doctor-test-email"><?php esc_html_e( 'Send test email to', 'mail-doctor' ); ?></label>
						<div class="mail-doctor-input-with-icon">
							<span class="dashicons dashicons-email"></span>
							<input type="email" id="mail-doctor-test-email" name="mail_doctor_test_email" class="regular-text"
								value="<?php echo esc_attr( $test_email ); ?>" placeholder="<?php esc_attr_e( 'Enter recipient email', 'mail-doctor' ); ?>" required />
						</div>
					</p>
					<p class="mail-doctor-input-group">
						<label for="mail-doctor-test-provider"><?php esc_html_e( 'Profile / Provider', 'mail-doctor' ); ?></label>
						<div class="mail-doctor-input-with-icon">
							<span class="dashicons dashicons-admin-settings"></span>
							<select id="mail-doctor-test-provider" name="mail_doctor_test_provider" class="mail-doctor-select mail-doctor-select--compact"
								data-placeholder="<?php esc_attr_e( 'Select provider', 'mail-doctor' ); ?>">
								<?php foreach ( $provider_options as $mail_doctor_provider_value => $mail_doctor_provider_label ) : ?>
									<option value="<?php echo esc_attr( $mail_doctor_provider_value ); ?>">
										<?php echo esc_html( $mail_doctor_provider_label ); ?>
									</option>
								<?php endforeach; ?>
							</select>
						</div>
					</p>
					<?php
					// Hidden configuration fields so quick actions can reuse saved credentials.
					$mail_doctor_hidden_fields = array(
						'mail-doctor-transport'          => $mail_doctor_quick_settings['transport'] ?? 'smtp',
						'mail-doctor-host'               => $mail_doctor_quick_settings['host'] ?? '',
						'mail-doctor-port'               => isset( $mail_doctor_quick_settings['port'] ) ? (string) $mail_doctor_quick_settings['port'] : '',
						'mail-doctor-encryption'         => $mail_doctor_quick_settings['encryption'] ?? '',
						'mail-doctor-username'           => $mail_doctor_quick_settings['username'] ?? '',
						'mail-doctor-password'           => $mail_doctor_quick_settings['password'] ?? '',
						'mail-doctor-from-email'         => $mail_doctor_quick_settings['from_email'] ?? '',
						'mail-doctor-from-name'          => $mail_doctor_quick_settings['from_name'] ?? '',
						'mail-doctor-mailgun-key'        => $mail_doctor_quick_settings['mailgun_api_key'] ?? '',
						'mail-doctor-mailgun-domain'     => $mail_doctor_quick_settings['mailgun_api_domain'] ?? '',
						'mail-doctor-mailgun-endpoint'   => $mail_doctor_quick_settings['mailgun_api_endpoint'] ?? '',
						'mail-doctor-sendgrid-key'       => $mail_doctor_quick_settings['sendgrid_api_key'] ?? '',
						'mail-doctor-postmark-token'     => $mail_doctor_quick_settings['postmark_api_token'] ?? '',
						'mail-doctor-sparkpost-key'      => $mail_doctor_quick_settings['sparkpost_api_key'] ?? '',
						'mail-doctor-sparkpost-endpoint' => $mail_doctor_quick_settings['sparkpost_api_endpoint'] ?? '',
						'mail-doctor-ses-key'            => $mail_doctor_quick_settings['ses_api_key'] ?? '',
						'mail-doctor-ses-secret'         => $mail_doctor_quick_settings['ses_api_secret'] ?? '',
						'mail-doctor-ses-token'          => $mail_doctor_quick_settings['ses_api_session_token'] ?? '',
						'mail-doctor-ses-region'         => $mail_doctor_quick_settings['ses_api_region'] ?? '',
						'mail-doctor-generic-endpoint'   => $mail_doctor_quick_settings['generic_api_endpoint'] ?? '',
						'mail-doctor-generic-method'     => $mail_doctor_quick_settings['generic_api_method'] ?? '',
						'mail-doctor-generic-token'      => $mail_doctor_quick_settings['generic_api_auth_token'] ?? '',
					);

					foreach ( $mail_doctor_hidden_fields as $mail_doctor_field_id => $mail_doctor_field_value ) :
						?>
						<input type="hidden" id="<?php echo esc_attr( $mail_doctor_field_id ); ?>"
							value="<?php echo esc_attr( (string) $mail_doctor_field_value ); ?>" />
					<?php endforeach; ?>
					<p>
						<button type="button"
							class="button button-primary mail-doctor-test-email-button"><?php esc_html_e( 'Send Test Email', 'mail-doctor' ); ?></button>
					</p>
				</form>
				<div class="mail-doctor-test-results" aria-live="polite">
					<p class="mail-doctor-test-placeholder">
						<?php esc_html_e( 'Results will appear here.', 'mail-doctor' ); ?>
					</p>
				</div>
			</div>

			<div class="mail-doctor-card mail-doctor-card--stacked">
				<h3><?php esc_html_e( 'Tips &amp; Insights', 'mail-doctor' ); ?></h3>
				<div class="mail-doctor-accordion">
					<details>
						<summary>
							<span class="dashicons dashicons-welcome-learn-more" aria-hidden="true"></span>
							<?php esc_html_e( 'Daily health checks', 'mail-doctor' ); ?>
						</summary>
						<ul class="mail-doctor-accordion__list">
							<li><?php esc_html_e( 'Run a status test email after changing SMTP credentials or DNS.', 'mail-doctor' ); ?>
							</li>
							<li><?php esc_html_e( 'Glance at queue totals for spikes in failed or quarantined sends.', 'mail-doctor' ); ?>
							</li>
							<li><?php esc_html_e( 'Confirm provider alerts are still routed to your on-call channel.', 'mail-doctor' ); ?>
							</li>
						</ul>
					</details>
					<details>
						<summary>
							<span class="dashicons dashicons-filter" aria-hidden="true"></span>
							<?php esc_html_e( 'Log analysis shortcuts', 'mail-doctor' ); ?>
						</summary>
						<ul class="mail-doctor-accordion__list">
							<li><?php esc_html_e( 'Filter by provider code to isolate throttling or auth issues quickly.', 'mail-doctor' ); ?>
							</li>
							<li><?php esc_html_e( 'Export failed deliveries to CSV and share with ESP support if needed.', 'mail-doctor' ); ?>
							</li>
							<li><?php esc_html_e( 'Use hashed search to trace GDPR-obfuscated recipients.', 'mail-doctor' ); ?>
							</li>
						</ul>
					</details>
					<details>
						<summary>
							<span class="dashicons dashicons-shield" aria-hidden="true"></span>
							<?php esc_html_e( 'Hardening checklist', 'mail-doctor' ); ?>
						</summary>
						<ul class="mail-doctor-accordion__list">
							<li><?php esc_html_e( 'Rotate API keys every 90 days and prune unused SMTP profiles.', 'mail-doctor' ); ?>
							</li>
							<li><?php esc_html_e( 'Enable fallback transport only after confirming WordPress mail is reliable.', 'mail-doctor' ); ?>
							</li>
							<li><?php esc_html_e( 'Audit retention windows to respect privacy requirements.', 'mail-doctor' ); ?>
							</li>
						</ul>
					</details>
				</div>
			</div>
		</aside>
	</div>
</div>
