<?php
/**
 * Email Report Section: Email Delivery
 *
 * @package EmailReports
 */

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

$primary_color = (string) ( $branding['primary_color'] ?? '#2271b1' );
$sent          = isset( $data['total_sent'] ) ? number_format_i18n( $data['total_sent'] ) : '0';
$failed        = isset( $data['total_failed'] ) ? number_format_i18n( $data['total_failed'] ) : '0';
$failed_color  = (int) ( $data['total_failed'] ?? 0 ) > 0 ? '#d63638' : $primary_color;

wpps_er_render_stat_card( $sent, __( 'Sent', 'wp-powersuite' ), '#00a32a' );
wpps_er_render_stat_card( $failed, __( 'Failed', 'wp-powersuite' ), $failed_color );

if ( ! empty( $data['recent_emails'] ) ) :
	wpps_er_render_subheading( __( 'Recent Emails', 'wp-powersuite' ) );
	foreach ( array_slice( $data['recent_emails'], 0, 5 ) as $row ) :
		$status       = (string) ( $row['status'] ?? '' );
		$status_color = $status === 'failed' ? '#d63638' : '#00a32a';
		wpps_er_render_list_item(
			(string) ( $row['subject'] ?? '' ),
			ucfirst( $status ),
			[ 'secondary_color' => $status_color ]
		);
	endforeach;
endif;

if ( (int) ( $data['total_sent'] ?? 0 ) === 0 && (int) ( $data['total_failed'] ?? 0 ) === 0 ) :
	?>
	<p style="color:#646970;margin:0;"><?php esc_html_e( 'No emails logged during this period.', 'wp-powersuite' ); ?></p>
	<?php
endif;
