<link href="<?php echo base_url('assets/css/admin-panel.css'); ?>?v=20260826b" rel="stylesheet">

<?php
$u = $user;
$tokenShort = $u['token'] !== '' ? (substr($u['token'], 0, 18) . '…') : '—';
$watch_logs = isset($watch_logs) && is_array($watch_logs) ? $watch_logs : array();
$latestLog = !empty($watch_logs) ? $watch_logs[0] : null;
?>

<div class="care-admin">
	<div class="ca-hero">
		<div class="ca-hero-inner row align-items-center g-3">
			<div class="col-lg-7">
				<p class="ca-kicker">Customers · Users</p>
				<h1 class="ca-title"><?php echo htmlspecialchars($u['userName'] !== '' ? $u['userName'] : 'User'); ?></h1>
				<p class="ca-sub mb-2">User ID: <code class="small"><?php echo htmlspecialchars($u['userId']); ?></code></p>
				<div class="d-flex flex-wrap gap-2 align-items-center">
					<span class="ca-role-chip"><?php echo htmlspecialchars($u['userRole'] !== '' ? $u['userRole'] : 'user'); ?></span>
					<?php if (!empty($u['isAdminVerified'])): ?>
						<span class="badge rounded-pill ca-pill bg-success" id="verifyBadge">Verified</span>
					<?php else: ?>
						<span class="badge rounded-pill ca-pill bg-warning text-dark" id="verifyBadge">Pending</span>
					<?php endif; ?>
				</div>
			</div>
			<div class="col-lg-5">
				<div class="ca-nav justify-content-lg-end flex-wrap">
					<a href="<?php echo base_url('admin/users'); ?>" class="btn btn-light"><i class="fa-solid fa-arrow-left me-2"></i>Back</a>
					<a href="<?php echo base_url('admin/user_edit/' . rawurlencode($u['uid'])); ?>" class="btn btn-primary"><i class="fa-regular fa-pen-to-square me-2"></i>Edit</a>
					<button type="button" class="btn btn-outline-danger" id="deleteUserBtn" data-uid="<?php echo htmlspecialchars($u['uid']); ?>" data-name="<?php echo htmlspecialchars($u['userName'] !== '' ? $u['userName'] : $u['email']); ?>">Delete</button>
				</div>
			</div>
		</div>
	</div>

	<div class="row g-3">
		<div class="col-lg-4">
			<div class="ca-card mb-3">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-0">Profile</h5>
				</div>
				<div class="ca-card-body">
					<p class="mb-2"><span class="text-muted small d-block">Username</span><?php echo htmlspecialchars($u['userName'] !== '' ? $u['userName'] : '—'); ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Email</span>
						<?php if ($u['email'] !== ''): ?>
							<a href="mailto:<?php echo htmlspecialchars($u['email']); ?>"><?php echo htmlspecialchars($u['email']); ?></a>
						<?php else: ?>
							—
						<?php endif; ?>
					</p>
					<p class="mb-2"><span class="text-muted small d-block">Device</span><?php echo htmlspecialchars($u['deviceType'] !== '' ? $u['deviceType'] : '—'); ?></p>
					<p class="mb-0"><span class="text-muted small d-block">Document ID</span><code class="small"><?php echo htmlspecialchars($u['uid']); ?></code></p>
				</div>
			</div>
			<div class="ca-card mb-3">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-0">Activity</h5>
				</div>
				<div class="ca-card-body">
					<p class="mb-2"><span class="text-muted small d-block">Coins</span><strong><?php echo (int) $u['points']; ?></strong></p>
					<p class="mb-2"><span class="text-muted small d-block">Ads watched</span><?php echo (int) $u['adWatchCount']; ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Last watch</span><?php echo htmlspecialchars($u['lastwatchDateLabel']); ?></p>
					<p class="mb-0"><span class="text-muted small d-block">Last question</span><?php echo htmlspecialchars($u['lastAskedQuestionLabel']); ?></p>
				</div>
			</div>
			<div class="ca-card">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-0">Account dates</h5>
				</div>
				<div class="ca-card-body">
					<p class="mb-2"><span class="text-muted small d-block">Created</span><?php echo htmlspecialchars($u['createdAtLabel']); ?></p>
					<p class="mb-2"><span class="text-muted small d-block">Updated</span><?php echo htmlspecialchars($u['updatedAtLabel']); ?></p>
					<p class="mb-0"><span class="text-muted small d-block">Push token</span><span class="small text-muted"><?php echo htmlspecialchars($tokenShort); ?></span></p>
				</div>
			</div>
		</div>
		<div class="col-lg-8">
			<div class="row g-3 mb-3">
				<div class="col-md-4">
					<div class="ca-stat h-100">
						<p class="ca-stat-label">Log entries</p>
						<p class="ca-stat-value mb-0"><?php echo count($watch_logs); ?></p>
					</div>
				</div>
				<div class="col-md-4">
					<div class="ca-stat h-100">
						<p class="ca-stat-label">Log ad count</p>
						<p class="ca-stat-value mb-0"><?php echo $latestLog ? (int) $latestLog['adCount'] : 0; ?></p>
					</div>
				</div>
				<div class="col-md-4">
					<div class="ca-stat h-100">
						<p class="ca-stat-label">Log coins</p>
						<p class="ca-stat-value mb-0"><?php echo $latestLog ? (int) $latestLog['points'] : 0; ?></p>
					</div>
				</div>
			</div>

			<div class="ca-card">
				<div class="ca-card-header">
					<h5 class="ca-card-title mb-1"><i class="fa-solid fa-clapperboard me-2 text-success"></i>Ad watch log history</h5>
					<p class="mb-0">From collection <code>ad_watch_log</code> for this user.</p>
				</div>
				<div class="ca-card-body p-0">
					<?php if (empty($watch_logs)): ?>
						<div class="p-4 text-center">
							<div class="mb-2"><i class="fa-regular fa-circle-pause fs-2 text-muted"></i></div>
							<div class="fw-semibold">No watch log yet</div>
							<div class="text-muted small">No documents in <code>ad_watch_log</code> for this userId.</div>
						</div>
					<?php else: ?>
						<div class="ca-table-wrap border-0 rounded-0 border-top">
							<table class="table ca-table table-striped mb-0 align-middle">
								<thead>
									<tr>
										<th>Last watch</th>
										<th>Ad count</th>
										<th>Coins</th>
										<th>User ID</th>
										<th class="text-muted small">Log ID</th>
									</tr>
								</thead>
								<tbody>
									<?php foreach ($watch_logs as $log): ?>
										<tr>
											<td><?php echo htmlspecialchars($log['lastwatchDateLabel']); ?></td>
											<td class="fw-semibold"><?php echo (int) $log['adCount']; ?></td>
											<td><?php echo (int) $log['points']; ?></td>
											<td class="small"><code><?php echo htmlspecialchars($log['userId']); ?></code></td>
											<td class="small text-muted"><?php echo htmlspecialchars($log['id']); ?></td>
										</tr>
									<?php endforeach; ?>
								</tbody>
							</table>
						</div>
					<?php endif; ?>
				</div>
			</div>
		</div>
	</div>
</div>

<script>
(function () {
	var deleteBtn = document.getElementById('deleteUserBtn');
	if (deleteBtn) {
		deleteBtn.addEventListener('click', function () {
			var uid = deleteBtn.getAttribute('data-uid');
			var name = deleteBtn.getAttribute('data-name') || uid;
			if (!uid) return;
			if (!confirm('Delete user "' + name + '" from Firestore and Firebase Auth?')) return;
			deleteBtn.disabled = true;
			fetch('<?php echo base_url('admin/user_delete/'); ?>' + encodeURIComponent(uid), {
				method: 'POST',
				headers: { 'Accept': 'application/json' }
			}).then(function (res) { return res.json(); }).then(function (json) {
				if (!json || !json.ok) throw new Error((json && json.error) ? json.error : 'Delete failed');
				window.location.href = '<?php echo base_url('admin/users'); ?>';
			}).catch(function (err) {
				deleteBtn.disabled = false;
				alert(err && err.message ? err.message : 'Delete failed');
			});
		});
	}
})();
</script>
