<?php
/**
 * Reports — unsubscribe reasons tab.
 *
 * @package CartCue
 */
defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template scope.

$aggregate = \CartCue\UnsubscribeReasons::get_aggregate( 90 );
$recent    = \CartCue\UnsubscribeReasons::get_recent( 50 );
?>
<div class="ap-card">
	<div class="ap-card__header"><?php esc_html_e( 'Unsubscribe reasons (last 90 days)', 'cartcue-sales-automation' ); ?></div>
	<div class="ap-card__body">
		<?php if ( empty( $aggregate ) ) : ?>
		<p class="description"><?php esc_html_e( 'No unsubscribe feedback collected yet. Reasons appear when customers use your unsubscribe link.', 'cartcue-sales-automation' ); ?></p>
		<?php else : ?>
		<table class="wp-list-table widefat fixed striped">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Reason', 'cartcue-sales-automation' ); ?></th>
					<th><?php esc_html_e( 'Count', 'cartcue-sales-automation' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $aggregate as $row ) : ?>
				<tr>
					<td><?php echo esc_html( $row['label'] ); ?></td>
					<td><?php echo esc_html( (string) $row['count'] ); ?></td>
				</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
		<?php endif; ?>
	</div>
</div>

<?php if ( ! empty( $recent ) ) : ?>
<div class="ap-card" style="margin-top:16px">
	<div class="ap-card__header"><?php esc_html_e( 'Recent responses', 'cartcue-sales-automation' ); ?></div>
	<div class="ap-card__body" style="padding:0">
		<table class="wp-list-table widefat fixed striped" style="border:0">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Email', 'cartcue-sales-automation' ); ?></th>
					<th><?php esc_html_e( 'Reason', 'cartcue-sales-automation' ); ?></th>
					<th><?php esc_html_e( 'Date', 'cartcue-sales-automation' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $recent as $row ) : ?>
				<tr>
					<td><?php echo esc_html( $row['email'] ); ?></td>
					<td><?php echo esc_html( \CartCue\UnsubscribeReasons::format_label( $row ) ); ?></td>
					<td><?php echo esc_html( $row['created_at'] ); ?></td>
				</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>
</div>
<?php endif; ?>
