<?php
session_start();

/* 🔥 CLEAR OLD ATTEMPT DATA */
unset($_SESSION['final_attempted']);
unset($_SESSION['final_score']);

/* ===============================
   GET topic_id (IMPORTANT)
=============================== */
$topic_id = $_GET['topic_id'] ?? 1;

/* ===============================
   TEMP STUDENT SESSION
=============================== */
if (!isset($_SESSION['student_id'])) {
    $_SESSION['student_id']   = $_GET['roll'] ?? '101';
    $_SESSION['student_name'] = $_GET['name'] ?? 'Student';
    $_SESSION['student_class'] = $_GET['class'] ?? '';
}

/* ===============================
   FINAL QUESTIONS
=============================== */
$questions = [
    [
        "question" => "Which of the following variable names will cause an error in Python?",
        "options" => ["count", "_value", "2value", "value2"],
        "answer" => "2value"
    ],
    [
        "question" => "What will be the output of: print(type(5))?",
        "options" => ["int", "<class 'int'>", "integer", "number"],
        "answer" => "<class 'int'>"
    ],
    [
        "question" => "Which statement correctly converts user input into integer?",
        "options" => [
            "x = input()",
            "x = int(input())",
            "x = input(int)",
            "x = readInt()"
        ],
        "answer" => "x = int(input())"
    ],
    [
        "question" => "Which condition will execute the block only if x is greater than 10?",
        "options" => [
            "if x = 10",
            "if x > 10",
            "if x >= 10",
            "if x != 10"
        ],
        "answer" => "if x > 10"
    ],
    [
        "question" => "How many times will the loop run? for i in range(1, 5):",
        "options" => ["3", "4", "5", "Infinite"],
        "answer" => "4"
    ]
];

/* ===============================
   FORM SUBMIT
=============================== */
if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $score = 0;
    $total = 0;

    foreach ($questions as $index => $q) {
        if (isset($_POST['q'.$index])) {
            $total++;
            if ($_POST['q'.$index] === $q['answer']) {
                $score++;
            }
        }
    }

    $percentage = ($score / $total) * 100;

    $_SESSION['final_attempted'] = true;
    $_SESSION['final_score'] = $percentage;

    /* 🔥 PASS topic_id TO RESULT PAGE */
    header("Location: /rs_lab/final_assessment_result.php?topic_id=$topic_id");
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Final Assessment | RS Learning Lab</title>
<style>
body{
    margin:0;
    background:radial-gradient(circle at top,#020617,#0f172a);
    font-family:Arial;
    color:#e5e7eb;
}
.assessment-container{
    max-width:800px;
    margin:60px auto;
    background:#020617;
    padding:40px;
    border-radius:22px;
    box-shadow:0 0 40px rgba(0,0,0,.7);
}
h2{
    text-align:center;
    margin-bottom:30px;
}
.question-box{
    margin-bottom:25px;
}
label{
    display:block;
    margin:6px 0;
}
.btn{
    display:block;
    margin:30px auto 0;
    padding:14px 40px;
    border:none;
    border-radius:999px;
    font-weight:bold;
    cursor:pointer;
    background:linear-gradient(135deg,#22c55e,#06b6d4);
    color:#020617;
    font-size:16px;
}
</style>
</head>
<body>

<div class="assessment-container">

<h2>🏁 Final Assessment</h2>

<form method="post">

<?php foreach ($questions as $index => $q): ?>
    <div class="question-box">
        <p><strong><?= ($index + 1) . ". " . htmlspecialchars($q['question']) ?></strong></p>

        <?php foreach ($q['options'] as $opt): ?>
            <label>
                <input type="radio"
                       name="q<?= $index ?>"
                       value="<?= htmlspecialchars($opt) ?>"
                       required>
                <?= htmlspecialchars($opt) ?>
            </label>
        <?php endforeach; ?>
    </div>
<?php endforeach; ?>

<button type="submit" class="btn">
Submit Final Assessment
</button>

</form>

</div>

</body>
</html>