<?php
require_once __DIR__ . '/guard.php';
require_login();

try {
	$db = new PDO('sqlite:' . dirname(__DIR__) . '/data/app.db');
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$db->exec('CREATE TABLE IF NOT EXISTS points (user_login TEXT PRIMARY KEY, display_name TEXT, points INTEGER DEFAULT 0)');
	$rows = $db->query('SELECT user_login, display_name, points FROM points ORDER BY points DESC LIMIT 200')->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
	die('Database error: ' . htmlspecialchars($e->getMessage()));
}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Loyalty Points</title>
	<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrap">
	<div class="card">
		<h2>🏆 Loyalty Points – Top</h2>
		<table>
			<tr>
				<th>#</th>
				<th>Bruger</th>
				<th>Point</th>
			</tr>
			<?php foreach ($rows as $i => $r): ?>
				<tr>
					<td><?= $i + 1 ?></td>
					<td>@<?= htmlspecialchars($r['display_name'] ?: $r['user_login']) ?></td>
					<td><?= (int)$r['points'] ?></td>
				</tr>
			<?php endforeach; ?>
		</table>
	</div>
</div>
</body>
</html>