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

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

$primary_color = (string) ( $branding['primary_color'] ?? '#2271b1' );
$total_404s    = isset( $data['total_404s'] ) ? number_format_i18n( $data['total_404s'] ) : '0';
$unique_urls   = isset( $data['unique_urls'] ) ? number_format_i18n( $data['unique_urls'] ) : '0';

wpps_er_render_stat_card( $total_404s, __( 'Total 404 Errors', 'wp-powersuite' ), $primary_color );
wpps_er_render_stat_card( $unique_urls, __( 'Unique URLs', 'wp-powersuite' ), $primary_color );

if ( ! empty( $data['top_404_urls'] ) ) :
	wpps_er_render_subheading( __( 'Most Common 404 URLs', 'wp-powersuite' ) );
	foreach ( array_slice( $data['top_404_urls'], 0, 5 ) as $item ) :
		$url  = is_array( $item ) ? ( $item['url'] ?? $item['path'] ?? '' ) : $item;
		$hits = is_array( $item ) ? ( $item['hits'] ?? $item['count'] ?? '' ) : '';
		/* translators: %s: string value. */
		$meta = $hits !== '' ? sprintf( __( '%s hits', 'wp-powersuite' ), number_format_i18n( $hits ) ) : '';
		wpps_er_render_code_list_item( (string) $url, $meta );
	endforeach;

	if ( (int) ( $data['total_404s'] ?? 0 ) > 50 ) :
		?>
		<table role="presentation" width="100%" cellspacing="0" cellpadding="0" style="width:100%;margin-top:12px;">
			<tr>
				<td style="padding:14px 16px;background-color:#fff8e5;border:1px solid #f0d58a;border-left:4px solid #dba617;border-radius:8px;color:#856404;font-size:13px;line-height:1.5;">
					<strong><?php esc_html_e( 'Tip:', 'wp-powersuite' ); ?></strong>
					<?php esc_html_e( 'Consider setting up redirects for frequently accessed 404 URLs to improve user experience and SEO.', 'wp-powersuite' ); ?>
				</td>
			</tr>
		</table>
		<?php
	endif;
else :
	?>
	<p style="color:#00a32a;margin:0;"><?php esc_html_e( 'No 404 errors recorded during this period.', 'wp-powersuite' ); ?></p>
	<?php
endif;

