<?php
/**
 * Dashboard view.
 *
 * @var array $data
 * @package SuiteMkt3k
 */
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

use SuiteMkt3k\Admin\View_Inline_Assets;
use SuiteMkt3k\Utils\Activity_Labels;

$kpis     = $data['stats']['kpis'] ?? array();
$counters = $data['counters'] ?? array();
$series   = $data['stats']['series'] ?? array();
$activity = $data['activity'] ?? array();

View_Inline_Assets::script_var(
	'SuiteMkt3kDashboard',
	array(
		'series' => $series,
		'labels' => array(
			'impressions' => __( 'Impressions', 'suitemkt3k' ),
			'clicks'      => __( 'Clicks', 'suitemkt3k' ),
			'opens'       => __( 'Opens', 'suitemkt3k' ),
			'conversions' => __( 'Conversions', 'suitemkt3k' ),
		),
	)
);
?>
<div class="wmk-grid wmk-grid-4">
	<div class="wmk-card wmk-stat">
		<div class="wmk-stat-label"><?php esc_html_e( 'Real subscribers', 'suitemkt3k' ); ?></div>
		<div class="wmk-stat-value"><?php echo esc_html( (string) ( $kpis['total_subscribers'] ?? 0 ) ); ?></div>
		<?php /* translators: %d: number of subscribers in the last 30 days. */ ?>
		<div class="wmk-stat-meta"><?php echo esc_html( sprintf( __( 'New in 30d: %d', 'suitemkt3k' ), (int) ( $kpis['new_subscribers'] ?? 0 ) ) ); ?></div>
	</div>
	<div class="wmk-card wmk-stat">
		<div class="wmk-stat-label"><?php esc_html_e( 'Coupons created', 'suitemkt3k' ); ?></div>
		<div class="wmk-stat-value"><?php echo esc_html( (string) ( $counters['coupons_total'] ?? 0 ) ); ?></div>
		<?php /* translators: %d: coupon clicks in the last 30 days. */ ?>
		<div class="wmk-stat-meta"><?php echo esc_html( sprintf( __( 'Clicks (30d): %d', 'suitemkt3k' ), (int) ( $kpis['clicks'] ?? 0 ) ) ); ?></div>
	</div>
	<div class="wmk-card wmk-stat">
		<div class="wmk-stat-label"><?php esc_html_e( 'Newsletters created', 'suitemkt3k' ); ?></div>
		<div class="wmk-stat-value"><?php echo esc_html( (string) ( $counters['newsletters_total'] ?? 0 ) ); ?></div>
		<?php /* translators: %d: newsletter opens in the last 30 days. */ ?>
		<div class="wmk-stat-meta"><?php echo esc_html( sprintf( __( 'Opens (30d): %d', 'suitemkt3k' ), (int) ( $kpis['opens'] ?? 0 ) ) ); ?></div>
	</div>
	<div class="wmk-card wmk-stat">
		<div class="wmk-stat-label"><?php esc_html_e( 'Carts pending recovery', 'suitemkt3k' ); ?></div>
		<div class="wmk-stat-value"><?php echo esc_html( (string) ( $counters['carts_pending'] ?? 0 ) ); ?></div>
		<?php /* translators: %d: recovered carts in the last 30 days. */ ?>
		<div class="wmk-stat-meta"><?php echo esc_html( sprintf( __( 'Recovered (30d): %d', 'suitemkt3k' ), (int) ( $kpis['carts_recovered'] ?? 0 ) ) ); ?></div>
	</div>
</div>

<div class="wmk-grid wmk-grid-2-3" style="margin-top:24px;">
	<div class="wmk-card">
		<div class="wmk-card-header">
			<h3><?php esc_html_e( 'Performance over time', 'suitemkt3k' ); ?></h3>
			<small><?php esc_html_e( 'Last 30 days', 'suitemkt3k' ); ?></small>
		</div>
		<div class="wmk-card-body">
			<canvas id="wmk-chart-overview" height="120"></canvas>
		</div>
	</div>
	<div class="wmk-card">
		<div class="wmk-card-header" style="display:flex;align-items:center;justify-content:space-between;gap:8px;">
			<h3><?php esc_html_e( 'Recent Activity', 'suitemkt3k' ); ?></h3>
			<a class="wmk-btn wmk-btn-ghost wmk-btn-sm" href="<?php echo esc_url( admin_url( 'admin.php?page=suitemkt3k-activity' ) ); ?>"><?php esc_html_e( 'View all', 'suitemkt3k' ); ?></a>
		</div>
		<div class="wmk-card-body">
			<ul class="wmk-activity">
			<?php foreach ( $activity as $log ) : ?>
				<li class="wmk-activity-item wmk-activity-<?php echo esc_attr( $log['level'] ); ?>">
					<div class="wmk-activity-dot"></div>
					<div class="wmk-activity-text">
						<strong><?php echo esc_html( Activity_Labels::context( (string) $log['context'] ) ); ?></strong>
						<span><?php echo esc_html( Activity_Labels::message( (string) $log['message'] ) ); ?></span>
						<small><?php echo esc_html( $log['created_at'] ); ?></small>
					</div>
				</li>
			<?php endforeach; ?>
			<?php if ( ! $activity ) : ?>
				<li class="wmk-activity-item"><?php esc_html_e( 'No recent activity yet.', 'suitemkt3k' ); ?></li>
			<?php endif; ?>
			</ul>
		</div>
	</div>
</div>

<?php require SUITEMKT3K_PATH . 'includes/admin/views/partials/donation-popup.php'; ?>
