<?php
/**
 * Dashboard alert band — operationally critical states shown at the top of the
 * populated dashboard (above the header bar).
 *
 * Surfaces conditions that an admin must notice immediately:
 *   - Card-Testing PANIC freeze (all checkouts blocked site-wide)
 *   - Card-Testing TARGETED state (fingerprints actively held)
 *   - Any chargeback network program over its network threshold
 *
 * Renders nothing when everything is healthy.
 *
 * @package TrustLens\Admin\Views\Partials
 * @since   1.2.2
 */

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View template; variables are scoped to the including method, not the global namespace.

$alerts = array();

// --- Card-Testing: Panic Freeze + Targeted ----------------------------
if ( class_exists( 'TrustLens_Lockdown_State' ) && class_exists( 'TrustLens_Admin_Card_Testing_Page' ) ) {
	$lockdown = new TrustLens_Lockdown_State();
	$ct_url   = admin_url( 'admin.php?page=' . TrustLens_Admin_Card_Testing_Page::PAGE_SLUG );

	if ( $lockdown->is_panic_active() ) {
		$expires   = $lockdown->panic_expires_at();
		$expires_h = $expires ? date_i18n( 'H:i', (int) $expires ) : '';
		$alerts[]  = array(
			'level'   => 'danger',
			'icon'    => 'shield-alt',
			'title'   => __( 'Panic Freeze Active', 'trustlens' ),
			'message' => $expires_h
				? sprintf(
					/* translators: %s: freeze-expiry clock time. */
					__( 'All new card attempts are being denied. Expires at %s.', 'trustlens' ),
					$expires_h
				)
				: __( 'All new card attempts are being denied.', 'trustlens' ),
			'cta_url'   => $ct_url,
			'cta_label' => __( 'Manage', 'trustlens' ),
		);
	} else {
		$targeted = $lockdown->currently_targeted();
		if ( ! empty( $targeted ) ) {
			$alerts[] = array(
				'level'   => 'warn',
				'icon'    => 'warning',
				'title'   => __( 'Card-testing attack in progress', 'trustlens' ),
				'message' => sprintf(
					/* translators: %d: number of fingerprints held. */
					_n(
						'%d fingerprint is being blocked at the checkout. Clean sessions unaffected.',
						'%d fingerprints are being blocked at the checkout. Clean sessions unaffected.',
						count( $targeted ),
						'trustlens'
					),
					count( $targeted )
				),
				'cta_url'   => $ct_url,
				'cta_label' => __( 'Review', 'trustlens' ),
			);
		}
	}
}

// --- Chargebacks: any program over threshold --------------------------
if ( wstl_has_chargebacks() && class_exists( 'TrustLens_Chargeback_Monitor' ) ) {
	$monitor = new TrustLens_Chargeback_Monitor();
	$ratios  = $monitor->get_ratios();
	$warn    = (int) get_option( 'trustlens_chargeback_alert_threshold_pct', 70 );
	$status  = TrustLens_Chargeback_Monitor::get_threshold_status( $ratios['calendar_month'], $warn );

	$over = array();
	foreach ( $status as $row ) {
		if ( 'danger' === ( $row['status'] ?? '' ) ) {
			$over[] = (string) ( $row['label'] ?? '' );
		}
	}

	if ( ! empty( $over ) ) {
		$alerts[] = array(
			'level'   => 'danger',
			'icon'    => 'chart-bar',
			'title'   => __( 'Chargeback ratio over network threshold', 'trustlens' ),
			'message' => sprintf(
				/* translators: %s: comma-separated program labels (e.g. Visa VDMP, Mastercard EWS). */
				__( 'Over the line for: %s. Review open disputes and respond immediately.', 'trustlens' ),
				implode( ', ', $over )
			),
			'cta_url'   => admin_url( 'admin.php?page=trustlens-chargeback-monitor' ),
			'cta_label' => __( 'Open Monitor', 'trustlens' ),
		);
	}
}

// --- Disputes due soon (open-dispute response deadlines) --------------
if ( function_exists( 'wstl_can_use_pro' ) && wstl_can_use_pro() && class_exists( 'TrustLens_Dispute_Repository' ) ) {
	$due_soon = ( new TrustLens_Dispute_Repository() )->count_open( 72 );
	if ( $due_soon > 0 ) {
		$alerts[] = array(
			'level'   => 'warn',
			'icon'    => 'clock',
			'title'   => __( 'Disputes need a response', 'trustlens' ),
			'message' => sprintf(
				/* translators: %d: number of disputes due within 3 days. */
				_n(
					'%d dispute is due within 3 days. Respond before the deadline to keep your chance to win.',
					'%d disputes are due within 3 days. Respond before the deadline to keep your chance to win.',
					$due_soon,
					'trustlens'
				),
				$due_soon
			),
			'cta_url'   => admin_url( 'admin.php?page=trustlens-chargeback-monitor' ),
			'cta_label' => __( 'Review open disputes', 'trustlens' ),
		);
	}
}

if ( empty( $alerts ) ) {
	return;
}
?>
<div class="wstl-dashboard-section wstl-alert-band">
	<?php foreach ( $alerts as $alert ) : ?>
		<div class="wstl-alert wstl-alert-<?php echo esc_attr( $alert['level'] ); ?>">
			<span class="dashicons dashicons-<?php echo esc_attr( $alert['icon'] ); ?>" aria-hidden="true"></span>
			<div class="wstl-alert-body">
				<strong><?php echo esc_html( $alert['title'] ); ?></strong>
				<span><?php echo esc_html( $alert['message'] ); ?></span>
			</div>
			<?php if ( ! empty( $alert['cta_url'] ) ) : ?>
				<a class="button button-small" href="<?php echo esc_url( $alert['cta_url'] ); ?>"><?php echo esc_html( $alert['cta_label'] ); ?></a>
			<?php endif; ?>
		</div>
	<?php endforeach; ?>
</div>
