<?php
/**
 * StoreSheet daily sync digest email (HTML).
 *
 * Override by copying to yourtheme/storesheet/emails/storesheet-sync-digest.php.
 *
 * @package StoreSheet
 *
 * @var array<string,mixed> $summary
 * @var string              $email_heading
 * @var \WC_Email           $email
 */

defined( 'ABSPATH' ) || exit;

do_action( 'woocommerce_email_header', $email_heading, $email );

$labels        = array(
	'product' => __( 'Products', 'storesheet' ),
	'order'   => __( 'Orders', 'storesheet' ),
	'coupon'  => __( 'Coupons', 'storesheet' ),
);
$resources     = isset( $summary['resources'] ) && is_array( $summary['resources'] ) ? $summary['resources'] : array();
$total_success = 0;
$total_skipped = 0;
foreach ( $labels as $key => $label ) {
	$total_success += (int) ( $resources[ $key ]['success'] ?? 0 );
	$total_skipped += (int) ( $resources[ $key ]['skipped'] ?? 0 );
}
$total_failed = (int) ( $summary['total_failed'] ?? 0 );
?>

<p><?php esc_html_e( 'Here is your StoreSheet sync activity for the last 24 hours.', 'storesheet' ); ?></p>

<table cellspacing="0" cellpadding="6" border="1" style="width:100%;border-collapse:collapse;">
	<tr>
		<th align="left"><?php esc_html_e( 'Type', 'storesheet' ); ?></th>
		<th align="left"><?php esc_html_e( 'Synced', 'storesheet' ); ?></th>
		<th align="left"><?php esc_html_e( 'Failed', 'storesheet' ); ?></th>
		<th align="left"><?php esc_html_e( 'Skipped', 'storesheet' ); ?></th>
	</tr>
	<?php foreach ( $labels as $key => $label ) : ?>
		<?php $r = isset( $resources[ $key ] ) && is_array( $resources[ $key ] ) ? $resources[ $key ] : array(); ?>
		<tr>
			<td><?php echo esc_html( $label ); ?></td>
			<td><?php echo (int) ( $r['success'] ?? 0 ); ?></td>
			<td><?php echo (int) ( $r['failed'] ?? 0 ); ?></td>
			<td><?php echo (int) ( $r['skipped'] ?? 0 ); ?></td>
		</tr>
	<?php endforeach; ?>
	<tr>
		<td><strong><?php esc_html_e( 'Total', 'storesheet' ); ?></strong></td>
		<td><strong><?php echo (int) $total_success; ?></strong></td>
		<td><strong><?php echo (int) $total_failed; ?></strong></td>
		<td><strong><?php echo (int) $total_skipped; ?></strong></td>
	</tr>
</table>

<?php if ( $total_failed > 0 ) : ?>
	<p><?php esc_html_e( 'Some syncs failed. Open WooCommerce → Status → Logs or StoreSheet → Overview to investigate.', 'storesheet' ); ?></p>
<?php endif; ?>

<?php
do_action( 'woocommerce_email_footer', $email );
