<?php
// ============================================
// RS Learning Lab - Certificate Verification
// ============================================

require_once 'db_config.php';

$verified = false;
$error = '';
$data = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $roll_no    = $_POST['roll_no'] ?? '';
    $concept_id = isset($_POST['concept_id']) ? (int)$_POST['concept_id'] : 0;

    if ($roll_no === '' || $concept_id === 0) {
        $error = "Please enter valid details.";
    } else {

        $stmt = $pdo->prepare("
        SELECT
            qa.roll_no,
            SUM(qa.is_correct) AS correct_count,
            COUNT(*) AS total_count
        FROM question_attempts qa
        WHERE qa.roll_no = ?
          AND qa.track = 'final'
        ");
        $stmt->execute([$roll_no]);
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        if ($row && $row['total_count'] > 0) {
            $percent = round(($row['correct_count'] / $row['total_count']) * 100);

            if ($percent >= 60) {
                $verified = true;
                $data = [
                    'roll_no' => $roll_no,
                    'concept' => $concept_id,
                    'score'   => $percent,
                    'date'    => date("d M Y")
                ];
            } else {
                $error = "Certificate not eligible (final not passed).";
            }
        } else {
            $error = "No final assessment found.";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Certificate Verification</title>
<style>
body {
    background:#020617;
    color:#e5e7eb;
    font-family:Arial;
}
.box {
    max-width:520px;
    margin:80px auto;
    background:#020617;
    padding:30px;
    border-radius:16px;
    border:1px solid #1e293b;
}
h2 { color:#38bdf8; text-align:center; }
input {
    width:100%;
    padding:10px;
    margin:10px 0;
    border-radius:8px;
    border:none;
}
button {
    width:100%;
    padding:12px;
    background:#0b8c9f;
    color:white;
    border:none;
    border-radius:8px;
    font-weight:bold;
    cursor:pointer;
}
.success {
    margin-top:20px;
    padding:15px;
    background:#022c22;
    border-radius:10px;
    color:#22c55e;
}
.error {
    margin-top:20px;
    padding:15px;
    background:#3f1d1d;
    border-radius:10px;
    color:#f87171;
}
</style>
</head>

<body>
<div class="box">

<h2>Certificate Verification</h2>

<form method="post">
    <input type="text" name="roll_no" placeholder="Student Roll No" required>
    <input type="number" name="concept_id" placeholder="Concept ID" required>
    <button type="submit">Verify Certificate</button>
</form>

<?php if ($verified): ?>
<div class="success">
    ✅ Certificate Verified<br><br>
    Roll No: <strong><?=htmlspecialchars($data['roll_no'])?></strong><br>
    Concept: <strong><?=htmlspecialchars($data['concept'])?></strong><br>
    Score: <strong><?=$data['score']?>%</strong><br>
    Status: <strong>VALID</strong><br>
    Date: <strong><?=$data['date']?></strong>
</div>
<?php endif; ?>

<?php if ($error): ?>
<div class="error">
    ❌ <?=$error?>
</div>
<?php endif; ?>

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