<?php
/**
 * Analytics page view.
 *
 * @package PrivexPasswordProtect
 */

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

// Check if user has active PRO license via Freemius.
$ppmstr_is_pro = function_exists( 'ppp_fs' ) && ppp_fs()->can_use_premium_code__premium_only();
?>

<div class="ppmstr-page-header">
	<h2>
		<?php esc_html_e( 'Analytics & Logs', 'privex-password-protect' ); ?>
		<?php if ( ! $ppmstr_is_pro ) : ?>
			<span class="ppmstr-pro-badge">PRO</span>
		<?php endif; ?>
	</h2>
	<p class="ppmstr-page-desc"><?php esc_html_e( 'Monitor password access attempts across your protected content over the last 7 days.', 'privex-password-protect' ); ?></p>
</div>

<?php /* fs_start_pro */ if ( $ppmstr_is_pro ) : ?>
	<?php
	// =====================================================================
	// PRO: Show real analytics data.
	// =====================================================================
	if ( ! class_exists( 'PPMSTR_Logger' ) ) {
		require_once PPMSTR_PLUGIN_DIR . 'includes/core/class-ppmstr-logger.php';
	}

	$ppmstr_stats         = PPMSTR_Logger::get_stats( 7 );
	$ppmstr_total_success = $ppmstr_stats['total_success'];
	$ppmstr_total_failed  = $ppmstr_stats['total_failed'];
	$ppmstr_total_all     = $ppmstr_total_success + $ppmstr_total_failed;
	$ppmstr_success_rate  = $ppmstr_total_all > 0 ? round( ( $ppmstr_total_success / $ppmstr_total_all ) * 100 ) : 0;
	?>

	<!-- Stats Row -->
	<div class="ppmstr-stats-row">
		<div class="ppmstr-stat-card">
			<div class="ppmstr-stat-icon ppmstr-stat-icon--primary">
				<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/></svg>
			</div>
			<div class="ppmstr-stat-info">
				<span class="ppmstr-stat-number"><?php echo absint( $ppmstr_total_all ); ?></span>
				<span class="ppmstr-stat-label"><?php esc_html_e( 'Total Attempts (7 Days)', 'privex-password-protect' ); ?></span>
			</div>
		</div>

		<div class="ppmstr-stat-card">
			<div class="ppmstr-stat-icon ppmstr-stat-icon--success">
				<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
			</div>
			<div class="ppmstr-stat-info">
				<span class="ppmstr-stat-number"><?php echo absint( $ppmstr_success_rate ); ?>%</span>
				<span class="ppmstr-stat-label"><?php esc_html_e( 'Success Rate', 'privex-password-protect' ); ?></span>
			</div>
		</div>

		<div class="ppmstr-stat-card">
			<div class="ppmstr-stat-icon ppmstr-stat-icon--warning">
				<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><line x1="12" y1="9" x2="12" y2="13"/><line x1="12" y1="17" x2="12.01" y2="17"/></svg>
			</div>
			<div class="ppmstr-stat-info">
				<span class="ppmstr-stat-number"><?php echo absint( $ppmstr_total_failed ); ?></span>
				<span class="ppmstr-stat-label"><?php esc_html_e( 'Failed Attempts', 'privex-password-protect' ); ?></span>
			</div>
		</div>
	</div>

	<!-- Chart Section -->
	<div class="ppmstr-card" style="margin-bottom: 24px;">
		<div class="ppmstr-card-header">
			<h3><?php esc_html_e( 'Activity Overview', 'privex-password-protect' ); ?></h3>
		</div>
		<div class="ppmstr-card-body">
			<div class="ppmstr-activity-chart" style="display:flex; height:200px; align-items:flex-end; gap:4%; border-bottom:1px solid #E4E6EF; padding-bottom:10px; margin-top:20px; position:relative;">
				<?php
				$ppmstr_max_val = 0;
				foreach ( $ppmstr_stats['daily_data'] as $ppmstr_day ) {
					$ppmstr_total = $ppmstr_day['success'] + $ppmstr_day['failed'];
					if ( $ppmstr_total > $ppmstr_max_val ) {
						$ppmstr_max_val = $ppmstr_total;
					}
				}
				$ppmstr_max_val = max( $ppmstr_max_val, 10 );

				foreach ( $ppmstr_stats['daily_data'] as $ppmstr_day ) {
					$ppmstr_s_h = ( $ppmstr_day['success'] / $ppmstr_max_val ) * 100;
					$ppmstr_f_h = ( $ppmstr_day['failed'] / $ppmstr_max_val ) * 100;
					$ppmstr_lbl = gmdate( 'M d', strtotime( $ppmstr_day['date'] ) );
					?>
					<div style="flex:1; display:flex; flex-direction:column; justify-content:flex-end; height:100%; position:relative;">
						<div style="width:100%; height:<?php echo esc_attr( $ppmstr_f_h ); ?>%; background:#FF7675; border-radius:4px 4px 0 0; opacity:0.8; transition:transform 0.2s;" title="Failed: <?php echo esc_attr( $ppmstr_day['failed'] ); ?>"></div>
						<div style="width:100%; height:<?php echo esc_attr( $ppmstr_s_h ); ?>%; background:#00B894; border-radius:<?php echo $ppmstr_f_h == 0 ? '4px 4px' : '0 0'; ?> 0 0; opacity:0.9; margin-top:1px; transition:transform 0.2s;" title="Success: <?php echo esc_attr( $ppmstr_day['success'] ); ?>"></div>
						<div style="position:absolute; bottom:-30px; left:0; width:100%; text-align:center; font-size:11px; color:#A0A5B8;"><?php echo esc_html( $ppmstr_lbl ); ?></div>
					</div>
					<?php
				}
				?>
			</div>
			<div style="display:flex; justify-content:center; gap:20px; margin-top:40px; font-size:12px; color:#6C7293;">
				<div style="display:flex; align-items:center; gap:6px;"><span style="width:12px;height:12px;background:#00B894;border-radius:3px;"></span> <?php esc_html_e( 'Successful Unlocks', 'privex-password-protect' ); ?></div>
				<div style="display:flex; align-items:center; gap:6px;"><span style="width:12px;height:12px;background:#FF7675;border-radius:3px;"></span> <?php esc_html_e( 'Failed Attempts', 'privex-password-protect' ); ?></div>
			</div>
		</div>
	</div>

	<!-- Recent Logs Table -->
	<div class="ppmstr-card">
		<div class="ppmstr-card-header">
			<h3><?php esc_html_e( 'Recent Access Logs', 'privex-password-protect' ); ?></h3>
		</div>
		<div class="ppmstr-card-body" style="padding: 0;">
			<table class="ppmstr-table" style="width:100%; text-align:left; border-collapse:collapse; font-size:13px;">
				<thead>
					<tr style="background:#F9F9FC; border-bottom:1px solid #E4E6EF;">
						<th style="padding:14px 20px; color:#A0A5B8; font-weight:600; text-transform:uppercase; font-size:11px;"><?php esc_html_e( 'Date', 'privex-password-protect' ); ?></th>
						<th style="padding:14px 20px; color:#A0A5B8; font-weight:600; text-transform:uppercase; font-size:11px;"><?php esc_html_e( 'Status', 'privex-password-protect' ); ?></th>
						<th style="padding:14px 20px; color:#A0A5B8; font-weight:600; text-transform:uppercase; font-size:11px;"><?php esc_html_e( 'IP Address (Masked)', 'privex-password-protect' ); ?></th>
						<th style="padding:14px 20px; color:#A0A5B8; font-weight:600; text-transform:uppercase; font-size:11px;"><?php esc_html_e( 'Context', 'privex-password-protect' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php if ( empty( $ppmstr_stats['recent_logs'] ) ) : ?>
						<tr>
							<td colspan="4" style="padding:30px; text-align:center; color:#A0A5B8;">
								<?php esc_html_e( 'No access logs recorded yet.', 'privex-password-protect' ); ?>
							</td>
						</tr>
					<?php else : ?>
						<?php foreach ( $ppmstr_stats['recent_logs'] as $ppmstr_log ) :
							$ppmstr_status_color = 'success' === $ppmstr_log['status'] ? '#00B894' : '#FF7675';
							$ppmstr_status_bg    = 'success' === $ppmstr_log['status'] ? 'rgba(0,184,148,0.1)' : 'rgba(255,118,117,0.1)';
							$ppmstr_context      = ucfirst( $ppmstr_log['type'] );
							if ( $ppmstr_log['target_id'] > 0 ) {
								if ( 'category' === $ppmstr_log['type'] ) {
									$term = get_term( $ppmstr_log['target_id'] );
									$ppmstr_context .= ' (' . ( $term && ! is_wp_error( $term ) ? $term->name : '#' . $ppmstr_log['target_id'] ) . ')';
								} elseif ( 'post' === $ppmstr_log['type'] || 'child' === $ppmstr_log['type'] ) {
									$ppmstr_context .= ' (' . get_the_title( $ppmstr_log['target_id'] ) . ')';
								}
							}
						?>
						<tr style="border-bottom:1px solid #F0F2F5;">
							<td style="padding:14px 20px; color:#6C7293;">
								<?php echo esc_html( wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $ppmstr_log['created_at'] ) ) ); ?>
							</td>
							<td style="padding:14px 20px;">
								<span style="background:<?php echo esc_attr( $ppmstr_status_bg ); ?>; color:<?php echo esc_attr( $ppmstr_status_color ); ?>; padding:4px 10px; border-radius:20px; font-size:11px; font-weight:600; text-transform:capitalize;">
									<?php echo esc_html( $ppmstr_log['status'] ); ?>
								</span>
							</td>
							<td style="padding:14px 20px; font-family:monospace; color:#1A1A2E;">
								<?php echo esc_html( $ppmstr_log['ip_address'] ); ?>
							</td>
							<td style="padding:14px 20px; color:#1A1A2E; font-weight:500;">
								<?php echo esc_html( $ppmstr_context ); ?>
							</td>
						</tr>
						<?php endforeach; ?>
					<?php endif; ?>
				</tbody>
			</table>
		</div>
	</div>

<?php else : ?>
	<?php
	// =====================================================================
	// FREE: Show blurred placeholder with PRO upgrade overlay.
	// =====================================================================
	?>
	<div style="position:relative; border-radius:16px; overflow:hidden;">

		<!-- Blurred placeholder preview (not real data) -->
		<div style="filter:blur(4px); pointer-events:none; user-select:none; opacity:0.45;">
			<div class="ppmstr-stats-row">
				<div class="ppmstr-stat-card">
					<div class="ppmstr-stat-icon ppmstr-stat-icon--primary">
						<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
					</div>
					<div class="ppmstr-stat-info">
						<span class="ppmstr-stat-number">248</span>
						<span class="ppmstr-stat-label"><?php esc_html_e( 'Total Attempts (7 Days)', 'privex-password-protect' ); ?></span>
					</div>
				</div>
				<div class="ppmstr-stat-card">
					<div class="ppmstr-stat-icon ppmstr-stat-icon--success">
						<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>
					</div>
					<div class="ppmstr-stat-info">
						<span class="ppmstr-stat-number">87%</span>
						<span class="ppmstr-stat-label"><?php esc_html_e( 'Success Rate', 'privex-password-protect' ); ?></span>
					</div>
				</div>
				<div class="ppmstr-stat-card">
					<div class="ppmstr-stat-icon ppmstr-stat-icon--warning">
						<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/></svg>
					</div>
					<div class="ppmstr-stat-info">
						<span class="ppmstr-stat-number">31</span>
						<span class="ppmstr-stat-label"><?php esc_html_e( 'Failed Attempts', 'privex-password-protect' ); ?></span>
					</div>
				</div>
			</div>
			<div class="ppmstr-card" style="margin-bottom:24px;">
				<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Activity Overview', 'privex-password-protect' ); ?></h3></div>
				<div class="ppmstr-card-body">
					<div style="display:flex; height:200px; align-items:flex-end; gap:4%; border-bottom:1px solid #E4E6EF; padding-bottom:10px; margin-top:20px;">
						<?php
						$ppmstr_fake_bars = array( 65, 80, 45, 90, 55, 70, 88 );
						$ppmstr_fake_fail = array( 10, 5, 15, 8, 20, 3, 12 );
						foreach ( $ppmstr_fake_bars as $ppmstr_i => $ppmstr_h ) :
						?>
						<div style="flex:1; display:flex; flex-direction:column; justify-content:flex-end; height:100%;">
							<div style="width:100%; height:<?php echo absint( $ppmstr_fake_fail[ $ppmstr_i ] ); ?>%; background:#FF7675; border-radius:4px 4px 0 0; opacity:0.8;"></div>
							<div style="width:100%; height:<?php echo absint( $ppmstr_h ); ?>%; background:#00B894; opacity:0.9; margin-top:1px;"></div>
						</div>
						<?php endforeach; ?>
					</div>
				</div>
			</div>
			<div class="ppmstr-card">
				<div class="ppmstr-card-header"><h3><?php esc_html_e( 'Recent Access Logs', 'privex-password-protect' ); ?></h3></div>
				<div class="ppmstr-card-body" style="padding:0;">
					<table style="width:100%; border-collapse:collapse; font-size:13px;">
						<thead><tr style="background:#F9F9FC; border-bottom:1px solid #E4E6EF;">
							<th style="padding:14px 20px; color:#A0A5B8; font-size:11px;"><?php esc_html_e( 'Date', 'privex-password-protect' ); ?></th>
							<th style="padding:14px 20px; color:#A0A5B8; font-size:11px;"><?php esc_html_e( 'Status', 'privex-password-protect' ); ?></th>
							<th style="padding:14px 20px; color:#A0A5B8; font-size:11px;"><?php esc_html_e( 'IP Address', 'privex-password-protect' ); ?></th>
							<th style="padding:14px 20px; color:#A0A5B8; font-size:11px;"><?php esc_html_e( 'Context', 'privex-password-protect' ); ?></th>
						</tr></thead>
						<tbody>
							<?php foreach ( array( array( 'May 10, 2026', 'success', '192.168.x.x', 'Post (Sample Page)' ), array( 'May 10, 2026', 'failed', '10.0.x.x', 'Site-wide' ), array( 'May 09, 2026', 'success', '203.0.x.x', 'Category (Members)' ) ) as $ppmstr_r ) : $ppmstr_ok = ( 'success' === $ppmstr_r[1] ); ?>
							<tr style="border-bottom:1px solid #F0F2F5;">
								<td style="padding:14px 20px; color:#6C7293;"><?php echo esc_html( $ppmstr_r[0] ); ?></td>
								<td style="padding:14px 20px;"><span style="background:<?php echo $ppmstr_ok ? 'rgba(0,184,148,0.1)' : 'rgba(255,118,117,0.1)'; ?>; color:<?php echo $ppmstr_ok ? '#00B894' : '#FF7675'; ?>; padding:4px 10px; border-radius:20px; font-size:11px; font-weight:600;"><?php echo esc_html( $ppmstr_r[1] ); ?></span></td>
								<td style="padding:14px 20px; font-family:monospace;"><?php echo esc_html( $ppmstr_r[2] ); ?></td>
								<td style="padding:14px 20px;"><?php echo esc_html( $ppmstr_r[3] ); ?></td>
							</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				</div>
			</div>
		</div><!-- end blurred preview -->

		<!-- PRO Upgrade Overlay -->
		<div style="position:absolute;inset:0;z-index:10;background:rgba(15,12,35,0.78);backdrop-filter:blur(2px);display:flex;flex-direction:column;align-items:center;justify-content:center;border-radius:16px;gap:14px;text-align:center;padding:40px;">
			<svg width="52" height="52" viewBox="0 0 24 24" fill="none" stroke="#A855F7" stroke-width="1.5"><path d="M22 12h-4l-3 9L9 3l-3 9H2"/></svg>
			<span class="ppmstr-pro-badge" style="font-size:13px; padding:5px 16px;"><?php esc_html_e( 'PRO Feature', 'privex-password-protect' ); ?></span>
			<h2 style="color:#fff; margin:0; font-size:22px; font-weight:700;"><?php esc_html_e( 'Analytics & Access Logs', 'privex-password-protect' ); ?></h2>
			<p style="color:rgba(255,255,255,0.65); font-size:14px; max-width:460px; margin:0; line-height:1.6;">
				<?php esc_html_e( 'Track every unlock attempt in real-time. See success rates, failed attempts, IP addresses, and daily trends across all your protected content.', 'privex-password-protect' ); ?>
			</p>
			<ul style="color:rgba(255,255,255,0.55); font-size:13px; text-align:left; margin:4px 0 0; padding:0; list-style:none; line-height:2.2;">
				<li>&#10022; <?php esc_html_e( '7-day activity chart with visual bars', 'privex-password-protect' ); ?></li>
				<li>&#10022; <?php esc_html_e( 'Detailed access log with IP masking', 'privex-password-protect' ); ?></li>
				<li>&#10022; <?php esc_html_e( 'Success vs. failed attempt tracking', 'privex-password-protect' ); ?></li>
				<li>&#10022; <?php esc_html_e( 'Per-post and per-category breakdown', 'privex-password-protect' ); ?></li>
			</ul>
			<?php if ( function_exists( 'ppp_fs' ) && ppp_fs()->is_not_paying() ) : ?>
			<a href="<?php echo esc_url( ppp_fs()->get_upgrade_url() ); ?>"
				style="display:inline-flex;align-items:center;gap:8px;margin-top:6px;background:linear-gradient(135deg,#6C5CE7,#A855F7);color:#fff;font-weight:700;font-size:14px;padding:12px 28px;border-radius:8px;text-decoration:none;box-shadow:0 4px 20px rgba(108,92,231,0.45);">
				&#9889; <?php esc_html_e( 'Upgrade to PRO', 'privex-password-protect' ); ?>
			</a>
			<?php endif; ?>
		</div>

	</div>
<?php endif; ?>
