<?php
/**
 * Magic Link digest email template.
 *
 * @package KaizenCoders\MagicLink\EmailDigest
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$site_name            = ! empty( $data['site_name'] ) ? $data['site_name'] : get_bloginfo( 'name' );
$frequency            = ! empty( $data['frequency'] ) ? ucfirst( (string) $data['frequency'] ) : __( 'Weekly', 'magic-link' );
$is_preview           = ! empty( $data['is_preview'] );
$is_pro               = ! empty( $data['is_pro'] );
$date_range_label     = ! empty( $data['date_range_label'] ) ? $data['date_range_label'] : '';
$generated_links      = isset( $data['generated_links'] ) ? (int) $data['generated_links'] : 0;
$used_links           = isset( $data['used_links'] ) ? (int) $data['used_links'] : 0;
$active_links         = isset( $data['active_links'] ) ? (int) $data['active_links'] : 0;
$expired_unused_links = isset( $data['expired_unused_links'] ) ? (int) $data['expired_unused_links'] : 0;
$unique_users         = isset( $data['unique_users'] ) ? (int) $data['unique_users'] : 0;
$use_rate             = isset( $data['use_rate'] ) ? $data['use_rate'] : null;
$top_users            = ! empty( $data['top_users'] ) && is_array( $data['top_users'] ) ? $data['top_users'] : [];
$recent_links         = ! empty( $data['recent_links'] ) && is_array( $data['recent_links'] ) ? $data['recent_links'] : [];
$news_items           = ! empty( $data['news_items'] ) && is_array( $data['news_items'] ) ? $data['news_items'] : [];
$dashboard_url        = admin_url( 'admin.php?page=magic-link-links' );
$settings_url         = admin_url( 'admin.php?page=kc-ml-settings' );
$docs_url             = 'https://kaizencoders.com/docs/magic-link/?utm_source=docs-link&utm_medium=email-digest&utm_campaign=magic-link-docs';
$upgrade_url          = 'https://kaizencoders.com/magic-link#pricing?utm_source=cta&utm_medium=email-digest&utm_campaign=magic-link-pro-upgrade';
$now_gmt              = gmdate( 'Y-m-d H:i:s', current_time( 'timestamp', true ) );

// Placeholder rows shown blurred to free users.
$placeholder_users = [
	[ 'name' => 'John Smith',   'email' => 'john@example.com',    'generated' => 12, 'used' => 9 ],
	[ 'name' => 'Sarah Connor', 'email' => 'sarah@example.com',   'generated' =>  8, 'used' => 6 ],
	[ 'name' => 'Alex Johnson', 'email' => 'alex@example.com',    'generated' =>  5, 'used' => 3 ],
];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo esc_html( $frequency ); ?> Digest</title>
</head>
<body style="margin:0;padding:24px;background:#f3f4f6;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;color:#1f2937;">
<div style="max-width:680px;margin:0 auto;background:#ffffff;border-radius:12px;overflow:hidden;border:1px solid #e5e7eb;">

	<!-- Header -->
	<div style="padding:32px;background:linear-gradient(135deg,#7c3aed 0%,#2563eb 100%);color:#ffffff;text-align:center;">
		<?php if ( $is_preview ) : ?>
			<div style="display:inline-block;margin-bottom:12px;padding:4px 10px;border-radius:999px;background:#fbbf24;color:#78350f;font-size:11px;font-weight:700;text-transform:uppercase;"><?php esc_html_e( 'Preview', 'magic-link' ); ?></div>
		<?php endif; ?>
		<div style="font-size:13px;letter-spacing:1.4px;text-transform:uppercase;opacity:.8;"><?php esc_html_e( 'Magic Link', 'magic-link' ); ?></div>
		<h1 style="margin:8px 0 0;font-size:28px;line-height:1.2;"><?php echo esc_html( $frequency ); ?> <?php esc_html_e( 'Activity Digest', 'magic-link' ); ?></h1>
		<p style="margin:10px 0 0;font-size:14px;opacity:.9;"><?php echo esc_html( $site_name ); ?> · <?php echo esc_html( $date_range_label ); ?></p>
	</div>

	<div style="padding:32px;">
		<p style="margin:0 0 20px;font-size:15px;line-height:1.6;">
			<?php esc_html_e( 'Here is your latest Magic Link activity summary. These numbers focus on generated login links and how they progressed during the selected period.', 'magic-link' ); ?>
		</p>

		<!-- Stat cards -->
		<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-bottom:24px;">
			<tr>
				<td width="48%" style="padding:0 0 12px;">
					<div style="background:#eff6ff;border-radius:10px;padding:18px;text-align:center;">
						<div style="font-size:34px;font-weight:700;color:#1d4ed8;"><?php echo esc_html( number_format_i18n( $generated_links ) ); ?></div>
						<div style="margin-top:6px;font-size:13px;color:#4b5563;"><?php esc_html_e( 'Links Generated', 'magic-link' ); ?></div>
					</div>
				</td>
				<td width="4%"></td>
				<td width="48%" style="padding:0 0 12px;">
					<div style="background:#ecfdf5;border-radius:10px;padding:18px;text-align:center;">
						<div style="font-size:34px;font-weight:700;color:#047857;"><?php echo esc_html( number_format_i18n( $used_links ) ); ?></div>
						<div style="margin-top:6px;font-size:13px;color:#4b5563;"><?php esc_html_e( 'Links Used', 'magic-link' ); ?></div>
					</div>
				</td>
			</tr>
			<tr>
				<td width="48%">
					<div style="background:#f5f3ff;border-radius:10px;padding:18px;text-align:center;">
						<div style="font-size:34px;font-weight:700;color:#6d28d9;"><?php echo esc_html( number_format_i18n( $active_links ) ); ?></div>
						<div style="margin-top:6px;font-size:13px;color:#4b5563;"><?php esc_html_e( 'Still Active', 'magic-link' ); ?></div>
					</div>
				</td>
				<td width="4%"></td>
				<td width="48%">
					<div style="background:#fff7ed;border-radius:10px;padding:18px;text-align:center;">
						<div style="font-size:34px;font-weight:700;color:#c2410c;"><?php echo esc_html( number_format_i18n( $expired_unused_links ) ); ?></div>
						<div style="margin-top:6px;font-size:13px;color:#4b5563;"><?php esc_html_e( 'Expired Unused', 'magic-link' ); ?></div>
					</div>
				</td>
			</tr>
		</table>

		<!-- At a glance -->
		<div style="margin-bottom:28px;padding:16px 18px;border:1px solid #e5e7eb;border-radius:10px;background:#fafafa;">
			<div style="font-size:13px;color:#6b7280;text-transform:uppercase;letter-spacing:.08em;margin-bottom:10px;"><?php esc_html_e( 'At a glance', 'magic-link' ); ?></div>
			<p style="margin:0;font-size:14px;line-height:1.7;">
				<?php
				printf(
					/* translators: 1: unique users count, 2: use rate sentence */
					esc_html__( 'Magic links were generated for %1$d users. %2$s', 'magic-link' ),
					$unique_users,
					null !== $use_rate
						? sprintf(
							/* translators: %d: percentage */
							esc_html__( '%d%% of the generated links were used.', 'magic-link' ),
							$use_rate
						)
						: esc_html__( 'No usage rate is available yet for this period.', 'magic-link' )
				);
				?>
			</p>
		</div>

		<!-- ── Top Users by Magic Link Requests ─────────────────────────────── -->
		<h2 style="margin:0 0 4px;font-size:15px;color:#111827;">
			<?php esc_html_e( 'Top Users by Magic Link Requests', 'magic-link' ); ?>
			<?php if ( ! $is_pro ) : ?>
				<span style="display:inline-block;margin-left:6px;padding:2px 7px;border-radius:999px;background:#ede9fe;color:#6d28d9;font-size:10px;font-weight:700;vertical-align:middle;text-transform:uppercase;">PRO</span>
			<?php endif; ?>
		</h2>
		<p style="margin:0 0 10px;font-size:12px;color:#9ca3af;">
			<?php if ( $is_pro ) : ?>
				<?php esc_html_e( 'Showing top 3 users for this period.', 'magic-link' ); ?>
			<?php else : ?>
				<?php esc_html_e( 'Upgrade to PRO to see who uses Magic Links the most.', 'magic-link' ); ?>
			<?php endif; ?>
		</p>

		<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-bottom:8px;border-collapse:collapse;">
			<thead>
				<tr>
					<th style="text-align:left;padding:10px 12px;background:#f9fafb;border-bottom:1px solid #e5e7eb;font-size:12px;color:#6b7280;"><?php esc_html_e( 'User', 'magic-link' ); ?></th>
					<th style="text-align:right;padding:10px 12px;background:#f9fafb;border-bottom:1px solid #e5e7eb;font-size:12px;color:#6b7280;"><?php esc_html_e( 'Generated', 'magic-link' ); ?></th>
					<th style="text-align:right;padding:10px 12px;background:#f9fafb;border-bottom:1px solid #e5e7eb;font-size:12px;color:#6b7280;"><?php esc_html_e( 'Used', 'magic-link' ); ?></th>
				</tr>
			</thead>
			<tbody>
			<?php if ( $is_pro ) : ?>
				<?php if ( ! empty( $top_users ) ) : ?>
					<?php foreach ( $top_users as $user_row ) : ?>
						<tr>
							<td style="padding:10px 12px;border-bottom:1px solid #f3f4f6;">
								<div style="font-weight:600;"><?php echo esc_html( ! empty( $user_row->display_name ) ? $user_row->display_name : __( 'Unknown user', 'magic-link' ) ); ?></div>
								<div style="font-size:12px;color:#6b7280;"><?php echo esc_html( ! empty( $user_row->user_email ) ? $user_row->user_email : '-' ); ?></div>
							</td>
							<td style="padding:10px 12px;text-align:right;border-bottom:1px solid #f3f4f6;"><?php echo esc_html( number_format_i18n( (int) $user_row->generated_count ) ); ?></td>
							<td style="padding:10px 12px;text-align:right;border-bottom:1px solid #f3f4f6;"><?php echo esc_html( number_format_i18n( (int) $user_row->used_count ) ); ?></td>
						</tr>
					<?php endforeach; ?>
				<?php else : ?>
					<tr>
						<td colspan="3" style="padding:14px 12px;text-align:center;color:#9ca3af;font-size:13px;"><?php esc_html_e( 'No data for this period.', 'magic-link' ); ?></td>
					</tr>
				<?php endif; ?>
			<?php else : ?>
				<?php foreach ( $placeholder_users as $placeholder ) : ?>
					<tr>
						<td style="padding:10px 12px;border-bottom:1px solid #f3f4f6;">
							<div style="font-weight:600;color:transparent;text-shadow:0 0 7px #6b7280;"><?php echo esc_html( $placeholder['name'] ); ?></div>
							<div style="font-size:12px;color:transparent;text-shadow:0 0 5px #9ca3af;"><?php echo esc_html( $placeholder['email'] ); ?></div>
						</td>
						<td style="padding:10px 12px;text-align:right;border-bottom:1px solid #f3f4f6;color:transparent;text-shadow:0 0 5px #9ca3af;"><?php echo esc_html( (string) $placeholder['generated'] ); ?></td>
						<td style="padding:10px 12px;text-align:right;border-bottom:1px solid #f3f4f6;color:transparent;text-shadow:0 0 5px #9ca3af;"><?php echo esc_html( (string) $placeholder['used'] ); ?></td>
					</tr>
				<?php endforeach; ?>
			<?php endif; ?>
			</tbody>
		</table>

		<!-- "See all with PRO" footer for free users -->
		<?php if ( ! $is_pro ) : ?>
			<div style="margin-bottom:28px;padding:12px 14px;background:#f5f3ff;border-radius:0 0 8px 8px;border:1px solid #e9d5ff;border-top:none;text-align:center;">
				<a href="<?php echo esc_url( $upgrade_url ); ?>" style="color:#7c3aed;font-size:13px;font-weight:600;text-decoration:none;">
					&#128274; <?php esc_html_e( 'Upgrade to PRO to see all top users &rarr;', 'magic-link' ); ?>
				</a>
			</div>
		<?php else : ?>
			<div style="margin-bottom:28px;"></div>
		<?php endif; ?>

		<!-- ── Recent Magic Link Activity ───────────────────────────────────── -->
		<h2 style="margin:0 0 12px;font-size:15px;color:#111827;"><?php esc_html_e( 'Recent Magic Link Activity', 'magic-link' ); ?></h2>
		<?php if ( ! empty( $recent_links ) ) : ?>
			<table width="100%" cellpadding="0" cellspacing="0" border="0" style="margin-bottom:28px;border-collapse:collapse;">
				<thead>
					<tr>
						<th style="text-align:left;padding:10px 12px;background:#f9fafb;border-bottom:1px solid #e5e7eb;font-size:12px;color:#6b7280;"><?php esc_html_e( 'User', 'magic-link' ); ?></th>
						<th style="text-align:left;padding:10px 12px;background:#f9fafb;border-bottom:1px solid #e5e7eb;font-size:12px;color:#6b7280;"><?php esc_html_e( 'Status', 'magic-link' ); ?></th>
						<th style="text-align:left;padding:10px 12px;background:#f9fafb;border-bottom:1px solid #e5e7eb;font-size:12px;color:#6b7280;"><?php esc_html_e( 'Created', 'magic-link' ); ?></th>
					</tr>
				</thead>
				<tbody>
				<?php foreach ( $recent_links as $link ) : ?>
					<?php
					$status = __( 'Active', 'magic-link' );
					$color  = '#2563eb';
					if ( ! empty( $link->used ) ) {
						$status = __( 'Used', 'magic-link' );
						$color  = '#047857';
					} elseif ( ! empty( $link->expires ) && $link->expires < $now_gmt ) {
						$status = __( 'Expired', 'magic-link' );
						$color  = '#c2410c';
					}
					?>
					<tr>
						<td style="padding:10px 12px;border-bottom:1px solid #f3f4f6;">
							<div style="font-weight:600;"><?php echo esc_html( ! empty( $link->display_name ) ? $link->display_name : __( 'Unknown user', 'magic-link' ) ); ?></div>
							<div style="font-size:12px;color:#6b7280;"><?php echo esc_html( ! empty( $link->user_email ) ? $link->user_email : '-' ); ?></div>
						</td>
						<td style="padding:10px 12px;border-bottom:1px solid #f3f4f6;">
							<span style="display:inline-block;padding:3px 10px;border-radius:999px;background:#f3f4f6;color:<?php echo esc_attr( $color ); ?>;font-size:12px;font-weight:600;"><?php echo esc_html( $status ); ?></span>
						</td>
						<td style="padding:10px 12px;border-bottom:1px solid #f3f4f6;font-size:13px;color:#4b5563;"><?php echo esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $link->created_at . ' GMT' ) ) ); ?></td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		<?php else : ?>
			<div style="margin-bottom:28px;padding:16px 18px;border-radius:10px;background:#f9fafb;color:#6b7280;">
				<?php esc_html_e( 'No magic link activity was recorded for this period.', 'magic-link' ); ?>
			</div>
		<?php endif; ?>

		<?php if ( ! empty( $news_items ) ) : ?>
			<?php foreach ( $news_items as $item ) : ?>
				<div style="margin-bottom:28px;padding:18px;border:1px solid #e5e7eb;border-radius:10px;background:#fafafa;">
					<div style="display:inline-block;margin-bottom:8px;padding:3px 8px;border-radius:999px;background:#ede9fe;color:#6d28d9;font-size:11px;font-weight:700;text-transform:uppercase;"><?php echo esc_html( $item['badge'] ); ?></div>
					<div style="font-size:16px;font-weight:700;color:#111827;"><?php echo esc_html( $item['title'] ); ?></div>
					<?php if ( ! empty( $item['description'] ) ) : ?>
						<p style="margin:8px 0 0;font-size:14px;line-height:1.6;color:#4b5563;"><?php echo esc_html( $item['description'] ); ?></p>
					<?php endif; ?>
					<?php if ( ! empty( $item['url'] ) ) : ?>
						<p style="margin:12px 0 0;"><a href="<?php echo esc_url( $item['url'] ); ?>" style="color:#2563eb;text-decoration:none;font-weight:600;"><?php echo esc_html( ! empty( $item['url_label'] ) ? $item['url_label'] : __( 'Learn more ->', 'magic-link' ) ); ?></a></p>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
		<?php endif; ?>

		<!-- ── Upgrade to PRO notice (free users only) ──────────────────────── -->
		<?php if ( ! $is_pro ) : ?>
			<div style="margin-bottom:24px;padding:20px 24px;background:linear-gradient(135deg,#f5f3ff 0%,#ede9fe 100%);border:1px solid #ddd6fe;border-radius:10px;text-align:center;">
				<div style="font-size:11px;font-weight:700;text-transform:uppercase;letter-spacing:.08em;color:#7c3aed;margin-bottom:8px;">&#11088; <?php esc_html_e( 'Unlock More with PRO', 'magic-link' ); ?></div>
				<p style="margin:0 0 14px;font-size:14px;line-height:1.6;color:#4c1d95;">
					<?php esc_html_e( 'Upgrade to Magic Link PRO for the activity heatmap, full top-user analytics, extended history, and advanced security features.', 'magic-link' ); ?>
				</p>
				<a href="<?php echo esc_url( $upgrade_url ); ?>" style="display:inline-block;padding:10px 22px;background:#7c3aed;color:#ffffff;text-decoration:none;border-radius:8px;font-weight:600;font-size:14px;">
					<?php esc_html_e( 'Upgrade to PRO &rarr;', 'magic-link' ); ?>
				</a>
			</div>
		<?php endif; ?>

		<!-- ── CTA button ────────────────────────────────────────────────────── -->
		<div style="text-align:center;margin:8px 0 8px;">
			<a href="<?php echo esc_url( $dashboard_url ); ?>" style="display:inline-block;padding:12px 24px;background:#2563eb;color:#ffffff;text-decoration:none;border-radius:8px;font-weight:600;"><?php esc_html_e( 'View Magic Links', 'magic-link' ); ?></a>
		</div>
	</div>

	<!-- Footer -->
	<div style="padding:20px 32px;border-top:1px solid #e5e7eb;background:#f9fafb;text-align:center;">
		<p style="margin:0 0 6px;font-size:12px;color:#6b7280;"><?php esc_html_e( 'You are receiving this email because email digest is enabled in Magic Link settings.', 'magic-link' ); ?></p>
		<p style="margin:0;font-size:12px;color:#6b7280;">
			<a href="<?php echo esc_url( $settings_url ); ?>" style="color:#4b5563;"><?php esc_html_e( 'Manage settings', 'magic-link' ); ?></a>
			&nbsp;·&nbsp;
			<a href="<?php echo esc_url( $docs_url ); ?>" style="color:#4b5563;"><?php esc_html_e( 'Documentation', 'magic-link' ); ?></a>
		</p>
	</div>
</div>
</body>
</html>
