<?php
/**
 * Right-column delivery status + checklist (Settings tab).
 *
 * @package EmailSmtp
 * @var array<string, mixed> $settings
 * @var array{key: string, label: string, tone: string, detail: string} $health
 * @var list<array{id: string, label: string, done: bool, hint: string}> $checklist
 * @var array{
 *   show?: bool,
 *   domain?: string,
 *   from_email?: string,
 *   score?: int,
 *   total?: int,
 *   checked_at?: string,
 *   checks?: list<array{id: string, label: string, tone: string, detail: string, tip?: string}>
 * } $domain_health
 * @var array<string, mixed> $diagnostics
 * @var bool $email_logs_enabled
 * @var string $email_logs_url
 * @var string $page_url
 * @var array<string, array<string, mixed>> $connections
 */

use EmailSmtp\Includes\ConnectionStore;
use EmailSmtp\Includes\ProviderRegistry;

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

$default_id    = sanitize_key( (string) ( $settings['default_connection_id'] ?? '' ) );
$default       = $connections[ $default_id ] ?? null;
$done_count    = count( array_filter( $checklist, static fn( $i ) => ! empty( $i['done'] ) ) );
$total         = count( $checklist );
$test_url      = add_query_arg( [ 'tab' => 'test' ], $page_url );
$domain_health = isset( $domain_health ) && is_array( $domain_health ) ? $domain_health : [];
$show_domain   = ! empty( $connections ) && ! empty( $domain_health['show'] ) && ! empty( $domain_health['checks'] );
$dh_score      = (int) ( $domain_health['score'] ?? 0 );
$dh_total      = (int) ( $domain_health['total'] ?? 0 );
$dh_domain     = (string) ( $domain_health['domain'] ?? '' );
$dh_checked    = '';
if ( ! empty( $domain_health['checked_at'] ) ) {
	$ts = strtotime( (string) $domain_health['checked_at'] );
	if ( $ts ) {
		$dh_checked = sprintf(
			/* translators: %s: human time diff */
			__( 'Checked %s ago', 'wp-powersuite' ),
			human_time_diff( $ts, time() )
		);
	}
}
?>

<aside class="wpps-ed-sidebar">
	<div class="wpps-ed-sidebar-sticky">
	<details class="wpps-ed-panel wpps-ed-details-panel wpps-ed-sidebar-panel" open>
		<summary class="wpps-ed-panel-title"><?php esc_html_e( 'Delivery status', 'wp-powersuite' ); ?></summary>
		<div class="wpps-ed-panel-body">
			<span class="wpps-ed-status wpps-ed-status--<?php echo esc_attr( $health['tone'] === 'success' ? 'connected' : ( $health['tone'] === 'danger' ? 'failed' : 'incomplete' ) ); ?>">
				<?php echo esc_html( $health['label'] ); ?>
			</span>
			<p class="description" style="margin:10px 0 0;"><?php echo esc_html( $health['detail'] ); ?></p>

			<?php if ( is_array( $default ) ) : ?>
				<?php
				$pid  = sanitize_key( (string) ( $default['provider'] ?? '' ) );
				$prov = ProviderRegistry::get( $pid );
				$icon = ConnectionStore::provider_icon_url( $pid );
				?>
				<div class="wpps-ed-sidebar-meta">
					<span class="wpps-ed-conn-provider">
						<?php if ( $icon !== '' ) : ?>
							<img src="<?php echo esc_url( $icon ); ?>" alt="" class="wpps-ed-provider-icon wpps-ed-provider-icon--sm" />
						<?php endif; ?>
						<span>
							<strong><?php echo esc_html( (string) ( $default['name'] ?? '' ) ); ?></strong><br />
							<span class="wpps-ed-muted"><?php echo esc_html( (string) ( $default['from_email'] ?? '' ) ); ?></span>
							<?php if ( $prov ) : ?>
								<br /><span class="wpps-ed-muted"><?php echo esc_html( $prov->get_name() ); ?></span>
							<?php endif; ?>
						</span>
					</span>
				</div>
				<p class="description" style="margin:12px 0 0;">
					<?php
					echo esc_html(
						sprintf(
							/* translators: %s: datetime or dash */
							__( 'Last success: %s', 'wp-powersuite' ),
							! empty( $diagnostics['last_success_at'] ) ? (string) $diagnostics['last_success_at'] : '—'
						)
					);
					?>
				</p>
			<?php endif; ?>

			<p style="margin:14px 0 0;">
				<a class="button button-secondary" href="<?php echo esc_url( $test_url ); ?>"><?php esc_html_e( 'Send test email', 'wp-powersuite' ); ?></a>
				<?php if ( $email_logs_enabled ) : ?>
					<a class="button" href="<?php echo esc_url( $email_logs_url ); ?>"><?php esc_html_e( 'Email Logs', 'wp-powersuite' ); ?></a>
				<?php endif; ?>
			</p>
		</div>
	</details>

	<details class="wpps-ed-panel wpps-ed-details-panel wpps-ed-sidebar-panel">
		<summary class="wpps-ed-panel-title">
			<?php
			echo esc_html(
				sprintf(
					/* translators: 1: done, 2: total */
					__( 'Checklist (%1$d/%2$d)', 'wp-powersuite' ),
					$done_count,
					$total
				)
			);
			?>
		</summary>
		<div class="wpps-ed-panel-body">
			<ol class="wpps-ed-checklist">
				<?php foreach ( $checklist as $item ) : ?>
					<li class="<?php echo ! empty( $item['done'] ) ? 'is-done' : 'is-todo'; ?>">
						<span class="wpps-ed-check" aria-hidden="true"></span>
						<span>
							<strong><?php echo esc_html( $item['label'] ); ?></strong>
							<em><?php echo esc_html( $item['hint'] ); ?></em>
						</span>
					</li>
				<?php endforeach; ?>
			</ol>
		</div>
	</details>

	<?php if ( $show_domain ) : ?>
		<details class="wpps-ed-panel wpps-ed-details-panel wpps-ed-sidebar-panel" id="wpps-ed-domain-health-panel">
			<summary class="wpps-ed-panel-title wpps-ed-sidebar-panel__summary">
				<span class="wpps-ed-sidebar-panel__summary-text"><?php esc_html_e( 'Domain health', 'wp-powersuite' ); ?></span>
				<button
					type="button"
					class="button button-small"
					id="wpps-ed-domain-health-refresh"
					data-label="<?php esc_attr_e( 'Refresh', 'wp-powersuite' ); ?>"
				>
					<?php esc_html_e( 'Refresh', 'wp-powersuite' ); ?>
				</button>
			</summary>
			<div class="wpps-ed-panel-body" id="wpps-ed-domain-health-body">
				<div class="wpps-ed-domain-health__summary">
					<div>
						<strong class="wpps-ed-domain-health__domain" id="wpps-ed-dh-domain"><?php echo esc_html( $dh_domain ); ?></strong>
						<span class="wpps-ed-domain-health__score" id="wpps-ed-dh-score">
							<?php
							echo esc_html(
								sprintf(
									/* translators: 1: passed, 2: total */
									__( '%1$d of %2$d checks passed', 'wp-powersuite' ),
									$dh_score,
									$dh_total
								)
							);
							?>
						</span>
					</div>
					<span class="wpps-ed-domain-health__checked" id="wpps-ed-dh-checked"><?php echo esc_html( $dh_checked ); ?></span>
				</div>
				<ul class="wpps-ed-domain-health" id="wpps-ed-dh-list">
					<?php foreach ( (array) ( $domain_health['checks'] ?? [] ) as $check ) : ?>
						<li class="wpps-ed-domain-health__item wpps-ed-domain-health__item--<?php echo esc_attr( (string) ( $check['tone'] ?? 'muted' ) ); ?>">
							<strong><?php echo esc_html( (string) ( $check['label'] ?? '' ) ); ?></strong>
							<span><?php echo esc_html( (string) ( $check['detail'] ?? '' ) ); ?></span>
							<?php if ( ! empty( $check['tip'] ) ) : ?>
								<em class="wpps-ed-domain-health__tip"><?php echo esc_html( (string) $check['tip'] ); ?></em>
							<?php endif; ?>
						</li>
					<?php endforeach; ?>
				</ul>
				<p class="description wpps-ed-domain-health__note">
					<?php esc_html_e( 'Public DNS for your primary From domain. Results are cached for about an hour; use Refresh after changing DNS.', 'wp-powersuite' ); ?>
				</p>
			</div>
		</details>
	<?php endif; ?>
	</div><!-- .wpps-ed-sidebar-sticky -->
</aside>
