<?php
declare(strict_types=1);

if (session_status() === PHP_SESSION_NONE) session_start();

require_once __DIR__ . '/../../includes/db.php';
if (is_file(__DIR__ . '/../../includes/auth.php')) {
    require_once __DIR__ . '/../../includes/auth.php';
    if (function_exists('require_role')) {
        require_role(['super_admin','admin','branch_admin']);
    }
}

$mysqli = $mysqli ?? ($conn ?? null);
if (!$mysqli instanceof mysqli) die('DB missing');

function e($v): string {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$sql = "
SELECT 
    ga.*,
    COALESCE(s.full_name, s.name, CONCAT('Student #', ga.student_id)) AS student_name,
    COALESCE(s.center_reg_no, s.reference_no, '') AS student_reg
FROM lms_game_attempts ga
LEFT JOIN studentss s ON s.id = ga.student_id
ORDER BY ga.finished_at DESC, ga.id DESC
LIMIT 300
";

$rows = [];
$res = $mysqli->query($sql);
while ($res && $r = $res->fetch_assoc()) {
    $rows[] = $r;
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Game Attempts</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">
<div class="container-fluid p-4">

    <div class="d-flex justify-content-between align-items-center mb-3">
        <div>
            <h3 class="mb-0">Game Attempts</h3>
            <div class="text-muted">Student game score history</div>
        </div>
        <a href="content.php" class="btn btn-outline-secondary btn-sm">← Back</a>
    </div>

    <div class="card">
        <div class="card-body table-responsive">
            <table class="table table-bordered table-hover align-middle">
                <thead class="table-light">
                    <tr>
                        <th>ID</th>
                        <th>Student</th>
                        <th>Reg No</th>
                        <th>Game ID</th>
                        <th>Score</th>
                        <th>Percent</th>
                        <th>Time</th>
                        <th>Status</th>
                        <th>Finished</th>
                    </tr>
                </thead>
                <tbody>
                <?php if (!$rows): ?>
                    <tr>
                        <td colspan="9" class="text-center text-muted">No attempts yet.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($rows as $r): ?>
                        <tr>
                            <td><?= (int)$r['id'] ?></td>
                            <td><?= e($r['student_name']) ?></td>
                            <td><?= e($r['student_reg']) ?></td>
                            <td><?= (int)$r['game_id'] ?></td>
                            <td>
                                <strong><?= (int)$r['score'] ?></strong> / <?= (int)$r['total'] ?>
                            </td>
                            <td><?= e($r['percent']) ?>%</td>
                            <td><?= (int)$r['time_sec'] ?>s</td>
                            <td>
                                <?php if ((int)$r['success'] === 1): ?>
                                    <span class="badge bg-success">PASS</span>
                                <?php else: ?>
                                    <span class="badge bg-danger">FAIL</span>
                                <?php endif; ?>
                            </td>
                            <td><?= e($r['finished_at']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>
</body>
</html>