<?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', 'wppowersuite-lite' ), $primary_color );
wpps_er_render_stat_card( $unique_urls, __( 'Unique URLs', 'wppowersuite-lite' ), $primary_color );

if ( ! empty( $data['top_404_urls'] ) ) :
	wpps_er_render_subheading( __( 'Most Common 404 URLs', 'wppowersuite-lite' ) );
	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', 'wppowersuite-lite' ), 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:', 'wppowersuite-lite' ); ?></strong>
					<?php esc_html_e( 'Consider setting up redirects for frequently accessed 404 URLs to improve user experience and SEO.', 'wppowersuite-lite' ); ?>
				</td>
			</tr>
		</table>
		<?php
	endif;
else :
	?>
	<p style="color:#00a32a;margin:0;"><?php esc_html_e( 'No 404 errors recorded during this period.', 'wppowersuite-lite' ); ?></p>
	<?php
endif;

