<?php
session_start();
require_once __DIR__ . "/config/db.php";

/* STUDENT DATA */
$name  = $_GET['name'] ?? 'Student';
$roll  = $_GET['roll'] ?? 'NA';
$class = $_GET['class'] ?? '';
$lang  = $_GET['lang'] ?? 'python';
$topic_id = $_GET['topic_id'] ?? 1;

/* INIT QUESTIONS */
if (!isset($_SESSION['practice_questions'])) {

    $stmt = $pdo->prepare("
        SELECT * FROM questions
        WHERE id IN (22, 23)
    ");
    $stmt->execute(); // ✅ FIXED

    $_SESSION['practice_questions'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
    $_SESSION['practice_index'] = 0;
}

/* CURRENT QUESTION */
$questions = $_SESSION['practice_questions'];
print_r($questions);
exit;

if (!isset($_SESSION['practice_index'])) {
    $_SESSION['practice_index'] = 0;
}

$index = $_SESSION['practice_index'];

if ($index >= count($questions)) {
    header("Location: practice_completed.php");
    exit;
}

$q = $questions[$index];
$qno = $index + 1;
?>

<!DOCTYPE html>
<html>
<head>
<style>
body{
    background:linear-gradient(135deg,#0b1020,#1e293b);
    color:#fff;
    font-family:sans-serif;
}

.card{
    width:55%;
    margin:60px auto;
    padding:35px;
    background:#121a33;
    border-radius:18px;
    box-shadow:0 10px 30px rgba(0,0,0,0.4);
}

h2{
    text-align:center;
    margin-bottom:20px;
}

.option{
    display:block;
    padding:14px;
    margin:12px 0;
    background:#1e293b;
    border-radius:10px;
    cursor:pointer;
    transition:0.2s;
}

.option:hover{
    background:#334155;
    transform:scale(1.02);
}

input[type="radio"]{
    margin-right:10px;
}

.btn{
    margin-top:25px;
    padding:12px 25px;
    background:linear-gradient(135deg,#22c55e,#06b6d4);
    border:none;
    border-radius:25px;
    cursor:pointer;
    font-weight:bold;
}
</style>
</head>

<body>

<div class="card">

<h2>🧠 Practice Track</h2>

<p style="opacity:0.7">
<?= htmlspecialchars($name) ?> | <?= strtoupper($lang) ?>
</p>

<h3>Q<?= $qno ?>. <?= htmlspecialchars($q['question']) ?></h3>

<form method="POST" action="practice_submit.php">

<label class="option">
<input type="radio" name="answer" value="A" required>
<?= htmlspecialchars($q['option_a'] ?? 'Option A missing') ?>
</label>

<label class="option">
<input type="radio" name="answer" value="B">
<?= htmlspecialchars($q['option_b'] ?? 'Option B missing') ?>
</label>

<label class="option">
<input type="radio" name="answer" value="C">
<?= htmlspecialchars($q['option_c'] ?? 'Option C missing') ?>
</label>

<label class="option">
<input type="radio" name="answer" value="D">
<?= htmlspecialchars($q['option_d'] ?? 'Option D missing') ?>
</label>

<input type="hidden" name="correct" value="<?= htmlspecialchars($q['correct_answer'] ?? 'A') ?>">
<input type="hidden" name="hint" value="<?= htmlspecialchars($q['hint'] ?? 'Think step by step 💡') ?>">

<button class="btn">Submit Answer</button>

</form>

</div>

</body>
</html>