<?php
/**
 * Statistics view with range + category selectors and per-entity breakdown.
 *
 * @var array  $data
 * @var string $range
 * @var string $category
 * @var int    $entity_id
 * @var string $nonce
 * @package SuiteMkt3k
 */
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

use SuiteMkt3k\Admin\View_Inline_Assets;

$kpis     = $data['kpis'] ?? array();
$series   = $data['series'] ?? array();
$entities = $data['entities'] ?? array();
$ranges = array(
	'7d'    => __( '7 days', 'suitemkt3k' ),
	'30d'   => __( '30 days', 'suitemkt3k' ),
	'90d'   => __( '90 days', 'suitemkt3k' ),
	'180d'  => __( '180 days', 'suitemkt3k' ),
	'total' => __( 'Total', 'suitemkt3k' ),
);
$categories = array(
	'all'         => __( 'All marketing', 'suitemkt3k' ),
	'coupons'     => __( 'Coupons', 'suitemkt3k' ),
	'newsletters' => __( 'Newsletters', 'suitemkt3k' ),
	'subscribers' => __( 'New subscribers', 'suitemkt3k' ),
	'cart'        => __( 'Abandoned carts', 'suitemkt3k' ),
);

View_Inline_Assets::script_var(
	'SuiteMkt3kStats',
	array(
		'series' => $series,
		'labels' => array(
			'impressions' => __( 'Impressions', 'suitemkt3k' ),
			'clicks'      => __( 'Clicks', 'suitemkt3k' ),
			'opens'       => __( 'Opens', 'suitemkt3k' ),
			'conversions' => __( 'Conversions', 'suitemkt3k' ),
		),
	)
);
?>
<div class="wmk-toolbar wmk-card" style="flex-direction:column;align-items:stretch;gap:12px;">
	<div>
		<span class="wmk-toolbar-label"><?php esc_html_e( 'Category', 'suitemkt3k' ); ?></span>
		<div class="wmk-toolbar-actions">
			<?php foreach ( $categories as $key => $label ) : ?>
				<a class="wmk-btn <?php echo $category === $key ? 'wmk-btn-primary' : 'wmk-btn-ghost'; ?>"
					href="<?php echo esc_url( add_query_arg( array( 'category' => $key, 'range' => $range, 'page' => 'suitemkt3k-statistics' ), admin_url( 'admin.php' ) ) ); ?>">
					<?php echo esc_html( $label ); ?>
				</a>
			<?php endforeach; ?>
		</div>
	</div>
	<div>
		<span class="wmk-toolbar-label"><?php esc_html_e( 'Time range', 'suitemkt3k' ); ?></span>
		<div class="wmk-toolbar-actions">
			<?php foreach ( $ranges as $key => $label ) : ?>
				<a class="wmk-btn <?php echo $range === $key ? 'wmk-btn-primary' : 'wmk-btn-ghost'; ?>"
					href="<?php echo esc_url( add_query_arg( array( 'range' => $key, 'category' => $category, 'page' => 'suitemkt3k-statistics' ), admin_url( 'admin.php' ) ) ); ?>">
					<?php echo esc_html( $label ); ?>
				</a>
			<?php endforeach; ?>
		</div>
	</div>
</div>

<?php if ( 'cart' === $category ) : ?>
<div class="wmk-grid wmk-grid-4">
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Carts recovered', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['carts_recovered'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Revenue recovered', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( number_format( (float) ( $kpis['revenue_recovered'] ?? 0 ), 2 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Recovery emails sent', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['emails_sent'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Email opens', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['cart_email_opens'] ?? 0 ) ); ?></div></div>
</div>
<?php elseif ( 'subscribers' === $category ) : ?>
<div class="wmk-grid wmk-grid-3">
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'New subscribers in period', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['new_subscribers'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'User unsubscribes in period', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['unsubscribed_users'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Total active subscribers', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['total_subscribers'] ?? 0 ) ); ?></div></div>
</div>
<?php elseif ( 'all' === $category ) : ?>
<div class="wmk-grid wmk-grid-4">
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s + %s)', __( 'Impressions', 'suitemkt3k' ), __( 'Coupons', 'suitemkt3k' ), __( 'Newsletters', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['impressions'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s + %s)', __( 'Clicks', 'suitemkt3k' ), __( 'Coupons', 'suitemkt3k' ), __( 'Newsletters', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['clicks'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s + %s)', __( 'Opens', 'suitemkt3k' ), __( 'Newsletters', 'suitemkt3k' ), __( 'Abandoned carts', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['opens'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s + %s)', __( 'Conversions', 'suitemkt3k' ), __( 'Coupons', 'suitemkt3k' ), __( 'Abandoned carts', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['conversions'] ?? 0 ) ); ?></div></div>
</div>

<div class="wmk-grid wmk-grid-4" style="margin-top:16px;">
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'New subscribers in period', 'suitemkt3k' ), __( 'all channels', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['new_subscribers'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'Recovery emails sent', 'suitemkt3k' ), __( 'Abandoned carts', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['cart_emails_sent'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'Carts recovered', 'suitemkt3k' ), __( 'Abandoned carts', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['carts_recovered'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'Revenue recovered', 'suitemkt3k' ), __( 'Abandoned carts', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( number_format( (float) ( $kpis['revenue_recovered'] ?? 0 ), 2 ) ); ?></div></div>
</div>
<?php elseif ( 'coupons' === $category ) : ?>
<div class="wmk-grid wmk-grid-4">
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'Impressions', 'suitemkt3k' ), __( 'Coupons', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['coupon_impressions'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'Clicks', 'suitemkt3k' ), __( 'Coupons', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['coupon_clicks'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'Conversions', 'suitemkt3k' ), __( 'Coupons', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['coupon_conversions'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'New subscribers in period', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['new_subscribers'] ?? 0 ) ); ?></div></div>
</div>
<?php elseif ( 'newsletters' === $category ) : ?>
<div class="wmk-grid wmk-grid-4">
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'Impressions', 'suitemkt3k' ), __( 'Newsletters', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['newsletter_impressions'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'Opens', 'suitemkt3k' ), __( 'Newsletters', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['newsletter_opens'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php echo esc_html( sprintf( '%s (%s)', __( 'Clicks', 'suitemkt3k' ), __( 'Newsletters', 'suitemkt3k' ) ) ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['newsletter_clicks'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Emails sent', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['newsletter_sent'] ?? 0 ) ); ?></div></div>
</div>
<?php else : ?>
<div class="wmk-grid wmk-grid-4">
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Impressions', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['impressions'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Clicks', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['clicks'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Opens', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['opens'] ?? 0 ) ); ?></div></div>
	<div class="wmk-card wmk-stat"><div class="wmk-stat-label"><?php esc_html_e( 'Conversions', 'suitemkt3k' ); ?></div><div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['conversions'] ?? 0 ) ); ?></div></div>
</div>
<?php endif; ?>

<div class="wmk-card" style="margin-top:24px;">
	<div class="wmk-card-header"><h3><?php esc_html_e( 'Events timeline', 'suitemkt3k' ); ?></h3></div>
	<div class="wmk-card-body"><canvas id="wmk-stats-chart" height="140"></canvas></div>
</div>

<?php if ( $entities ) : ?>
<div class="wmk-card" style="margin-top:24px;">
	<div class="wmk-card-header"><h3>
		<?php echo esc_html( 'coupons' === $category ? __( 'Performance per coupon', 'suitemkt3k' ) : __( 'Performance per newsletter', 'suitemkt3k' ) ); ?>
	</h3></div>
	<table class="wmk-table">
		<thead><tr>
			<th>#</th>
			<th><?php esc_html_e( 'Name', 'suitemkt3k' ); ?></th>
			<?php if ( 'coupons' === $category ) : ?>
				<th><?php esc_html_e( 'Uses', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Impressions', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Clicks', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Conversions', 'suitemkt3k' ); ?></th>
			<?php else : ?>
				<th><?php esc_html_e( 'Impressions', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Opens', 'suitemkt3k' ); ?></th>
				<th><?php esc_html_e( 'Clicks', 'suitemkt3k' ); ?></th>
			<?php endif; ?>
		</tr></thead>
		<tbody>
			<?php foreach ( $entities as $e ) : ?>
			<tr>
				<td>#<?php echo (int) $e['id']; ?></td>
				<td><strong><?php echo esc_html( (string) $e['name'] ); ?></strong></td>
				<?php if ( 'coupons' === $category ) : ?>
					<td><?php echo (int) ( $e['usage_count'] ?? 0 ); ?></td>
					<td><?php echo (int) $e['impressions']; ?></td>
					<td><?php echo (int) $e['clicks']; ?></td>
					<td><?php echo (int) $e['conversions']; ?></td>
				<?php else : ?>
					<td><?php echo (int) $e['impressions']; ?></td>
					<td><?php echo (int) ( $e['opens'] ?? 0 ); ?></td>
					<td><?php echo (int) $e['clicks']; ?></td>
				<?php endif; ?>
			</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
</div>
<?php endif; ?>

