<?php
/**
 * Cockpit tab. $plugin is in scope (Ffrd_Admin::render).
 *
 * @package Form_Flight_Recorder
 */

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

$ffrd_counts   = $plugin->get( 'reconciler' )->counts();
$ffrd_detected = $plugin->get( 'adapters' )->detected();
$ffrd_events   = $plugin->get( 'events' );

if ( ! $ffrd_detected ) {
	echo '<div class="ffrd-panel">';
	echo '<h2>' . esc_html__( 'Waiting for a form plugin', 'form-flight-recorder' ) . '</h2>';
	echo '<p>' . esc_html__( 'Form Flight Recorder watches Contact Form 7, WPForms, Gravity Forms, Ninja Forms and Fluent Forms. Activate one and this page becomes your reconciliation cockpit.', 'form-flight-recorder' ) . '</p>';
	echo '</div>';
	return;
}

// Recent submissions + a lookup of which produced a missing-notification event.
$ffrd_recent  = $ffrd_events->query(
	array(
		'type'  => 'submission',
		'since' => time() - 7 * DAY_IN_SECONDS,
		'limit' => 15,
	)
);
$ffrd_missing = array();
foreach ( $ffrd_events->query(
	array(
		'type'  => 'notification_missing',
		'since' => time() - 7 * DAY_IN_SECONDS,
		'limit' => 200,
	)
) as $ffrd_row ) {
	if ( isset( $ffrd_row->data['submission'] ) ) {
		$ffrd_missing[ (int) $ffrd_row->data['submission'] ] = true;
	}
}
?>

<div class="ffrd-cards">
	<div class="ffrd-card">
		<span class="ffrd-num"><?php echo esc_html( number_format_i18n( $ffrd_counts['submissions_7d'] ) ); ?></span>
		<span class="ffrd-label"><?php esc_html_e( 'submissions (7 days)', 'form-flight-recorder' ); ?></span>
	</div>
	<div class="ffrd-card <?php echo esc_attr( $ffrd_counts['missing_7d'] ? 'ffrd-card-bad' : 'ffrd-card-good' ); ?>">
		<span class="ffrd-num"><?php echo esc_html( number_format_i18n( $ffrd_counts['missing_7d'] ) ); ?></span>
		<span class="ffrd-label"><?php esc_html_e( 'produced NO notification', 'form-flight-recorder' ); ?></span>
	</div>
	<div class="ffrd-card <?php echo esc_attr( $ffrd_counts['failed_7d'] ? 'ffrd-card-bad' : 'ffrd-card-good' ); ?>">
		<span class="ffrd-num"><?php echo esc_html( number_format_i18n( $ffrd_counts['failed_7d'] ) ); ?></span>
		<span class="ffrd-label"><?php esc_html_e( 'failed at the mail transport', 'form-flight-recorder' ); ?></span>
	</div>
	<div class="ffrd-card <?php echo esc_attr( $ffrd_counts['silent'] ? 'ffrd-card-bad' : '' ); ?>">
		<span class="ffrd-num"><?php echo esc_html( number_format_i18n( count( $ffrd_counts['silent'] ) ) ); ?></span>
		<span class="ffrd-label"><?php esc_html_e( 'forms gone silent', 'form-flight-recorder' ); ?></span>
	</div>
</div>

<?php if ( $ffrd_counts['silent'] ) : ?>
<div class="ffrd-panel ffrd-panel-warn">
	<h2><?php esc_html_e( 'Forms gone silent', 'form-flight-recorder' ); ?></h2>
	<p class="description"><?php esc_html_e( 'These forms normally receive submissions but have been quiet beyond their threshold. A broken form looks exactly like a quiet day — check the page, recent updates, and the spam filter.', 'form-flight-recorder' ); ?></p>
	<table class="widefat striped">
		<thead><tr>
			<th><?php esc_html_e( 'Form', 'form-flight-recorder' ); ?></th>
			<th><?php esc_html_e( 'Normally', 'form-flight-recorder' ); ?></th>
			<th><?php esc_html_e( 'Silent for', 'form-flight-recorder' ); ?></th>
			<th><?php esc_html_e( 'Last submission (UTC)', 'form-flight-recorder' ); ?></th>
		</tr></thead>
		<tbody>
		<?php foreach ( $ffrd_counts['silent'] as $ffrd_row ) : ?>
			<tr>
				<td><?php echo esc_html( isset( $ffrd_row['title'] ) ? $ffrd_row['title'] : '' ); ?></td>
				<td>
					<?php
					printf(
						/* translators: %s: submissions per week. */
						esc_html__( '~%s/week', 'form-flight-recorder' ),
						esc_html( isset( $ffrd_row['per_week'] ) ? (string) $ffrd_row['per_week'] : '?' )
					);
					?>
				</td>
				<td>
					<?php
					printf(
						/* translators: %d: hours. */
						esc_html__( '%dh', 'form-flight-recorder' ),
						(int) ( isset( $ffrd_row['hours_silent'] ) ? $ffrd_row['hours_silent'] : 0 )
					);
					?>
				</td>
				<td><?php echo esc_html( isset( $ffrd_row['last_at'] ) ? $ffrd_row['last_at'] : '' ); ?></td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>
</div>
<?php endif; ?>

<div class="ffrd-panel">
	<h2><?php esc_html_e( 'Recent submissions, reconciled', 'form-flight-recorder' ); ?></h2>
	<p class="description"><?php esc_html_e( '"Delivered to the mail system" means the site generated the notification and handed it off (or the form plugin confirmed its send). What happens after hand-off belongs to your mail transport.', 'form-flight-recorder' ); ?></p>
	<?php if ( $ffrd_recent ) : ?>
	<table class="widefat striped">
		<thead><tr>
			<th><?php esc_html_e( 'When (UTC)', 'form-flight-recorder' ); ?></th>
			<th><?php esc_html_e( 'Form', 'form-flight-recorder' ); ?></th>
			<th><?php esc_html_e( 'Plugin', 'form-flight-recorder' ); ?></th>
			<th><?php esc_html_e( 'Notifications', 'form-flight-recorder' ); ?></th>
			<th><?php esc_html_e( 'Status', 'form-flight-recorder' ); ?></th>
		</tr></thead>
		<tbody>
		<?php
		foreach ( $ffrd_recent as $ffrd_row ) :
			$ffrd_attempts = isset( $ffrd_row->data['attempts'] ) ? (int) $ffrd_row->data['attempts'] : 0;
			$ffrd_expects  = ! empty( $ffrd_row->data['expects'] );
			$ffrd_failed   = ! empty( $ffrd_row->data['failures'] );
			$ffrd_ok       = $ffrd_attempts > 0 || ! empty( $ffrd_row->data['sent_confirmed'] );

			if ( $ffrd_failed ) {
				$ffrd_badge = array( 'failed', __( 'send failed', 'form-flight-recorder' ) );
			} elseif ( isset( $ffrd_missing[ (int) $ffrd_row->id ] ) ) {
				$ffrd_badge = array( 'failed', __( 'NO notification', 'form-flight-recorder' ) );
			} elseif ( $ffrd_ok ) {
				$ffrd_badge = array( 'complete', __( 'delivered to mail system', 'form-flight-recorder' ) );
			} elseif ( ! $ffrd_expects ) {
				$ffrd_badge = array( '', __( 'none expected', 'form-flight-recorder' ) );
			} else {
				$ffrd_badge = array( '', __( 'awaiting reconciliation', 'form-flight-recorder' ) );
			}
			?>
			<tr>
				<td><?php echo esc_html( $ffrd_row->created_at ); ?></td>
				<td><?php echo esc_html( isset( $ffrd_row->data['title'] ) && '' !== $ffrd_row->data['title'] ? $ffrd_row->data['title'] : $ffrd_row->object_ref ); ?></td>
				<td><?php echo esc_html( isset( $ffrd_row->data['plugin'] ) ? $ffrd_row->data['plugin'] : '' ); ?></td>
				<td><?php echo esc_html( (string) $ffrd_attempts ); ?></td>
				<td><span class="ffrd-badge <?php echo esc_attr( $ffrd_badge[0] ? 'ffrd-badge-' . $ffrd_badge[0] : '' ); ?>"><?php echo esc_html( $ffrd_badge[1] ); ?></span></td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>
	<?php else : ?>
	<p><?php esc_html_e( 'No submissions recorded yet — the recorder starts with the next submission on any detected form.', 'form-flight-recorder' ); ?></p>
	<?php endif; ?>
</div>

<div class="ffrd-panel">
	<h2><?php esc_html_e( 'Watching', 'form-flight-recorder' ); ?></h2>
	<p>
		<?php
		printf(
			/* translators: 1: detected plugin list, 2: number of known forms. */
			esc_html__( 'Detected: %1$s — %2$d form(s) in the registry.', 'form-flight-recorder' ),
			esc_html( implode( ', ', $ffrd_detected ) ),
			(int) $ffrd_counts['forms']
		);
		?>
	</p>
</div>

<?php if ( function_exists( 'ffrd_fs' ) && ! ffrd_fs()->can_use_premium_code() ) : ?>
<div class="ffrd-panel ffrd-upsell">
	<h2><?php esc_html_e( 'Pro: catch the break before the leads stop', 'form-flight-recorder' ); ?></h2>
	<ul>
		<li><?php esc_html_e( 'Page-presence check — daily proof each form still renders on its page after updates.', 'form-flight-recorder' ); ?></li>
		<li><?php esc_html_e( 'Slack & webhook alerts the moment a notification goes missing or a form goes silent.', 'form-flight-recorder' ); ?></li>
		<li><?php esc_html_e( 'Weekend-aware baselines — no false alarms on Monday morning.', 'form-flight-recorder' ); ?></li>
		<li><?php esc_html_e( 'REST status endpoint + CSV export for agencies, 90-day history.', 'form-flight-recorder' ); ?></li>
	</ul>
	<p>
		<a class="button button-primary" href="<?php echo esc_url( ffrd_fs()->get_trial_url() ); ?>"><?php esc_html_e( 'Start 14-day free trial — no card', 'form-flight-recorder' ); ?></a>
		<a class="button" href="<?php echo esc_url( ffrd_fs()->get_upgrade_url() ); ?>"><?php esc_html_e( 'See plans', 'form-flight-recorder' ); ?></a>
	</p>
</div>
<?php endif; ?>
