<?php
session_start();
require_once 'db.php';

if (!isset($_SESSION['started'])) {
    header("Location: portal.php");
    exit();
}

$answers = isset($_POST['answers']) ? $_POST['answers'] : [];
$score = 0;

if (!empty($_SESSION['questions'])) {
    foreach ($_SESSION['questions'] as $q) {
        $q_id = $q['id'];
        
        // STABLE MATCHING: Grab the answer directly out of the secure loaded session
        $correct = isset($q['correct_option']) ? trim(strtoupper($q['correct_option'])) : '';
        $student_choice = isset($answers[$q_id]) ? trim(strtoupper($answers[$q_id])) : '';
        
        if (!empty($correct) && $student_choice === $correct) { 
            $score++; 
        }
    }
}

$total = isset($_SESSION['total_questions']) ? intval($_SESSION['total_questions']) : count($_SESSION['questions']);
$percentage = ($total > 0) ? ($score / $total) * 100 : 0;
$status = ($percentage >= 40) ? 'Passed' : 'Failed';

try {
    $insert = $pdo->prepare("INSERT INTO results (student_id, course_id, matric_number, student_name, sex, department, level, course_code, total_questions, score, percentage, exam_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $insert->execute([
        $_SESSION['student_id'], $_SESSION['course_id'], $_SESSION['matric_number'],
        $_SESSION['student_name'], $_SESSION['sex'], $_SESSION['department'],
        $_SESSION['student_level'], $_SESSION['course_code'], $total, $score, $percentage, $status
    ]);
} catch (\PDOException $e) {
    // Silently process structural duplicates to ensure local stability
}

$print_name   = $_SESSION['student_name'];
$print_matric = $_SESSION['matric_number'];
$print_course = $_SESSION['course_code'];

session_unset();
session_destroy();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Exam Submission Summary</title>
    <style>
        body { font-family: Arial, sans-serif; background-color: #f4f6f9; text-align: center; padding: 40px 20px; }
        .receipt-card { max-width: 480px; background: white; margin: 40px auto; padding: 35px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); border-top: 5px solid #2cb67d; }
        .metric { font-size: 48px; font-weight: bold; color: #333; margin: 15px 0; }
        .badge { display: inline-block; padding: 6px 16px; border-radius: 20px; font-weight: bold; color: white; font-size: 14px; }
        .pass { background: #2cb67d; } .fail { background: #d9534f; }
        .btn { padding: 12px 24px; background: #007bff; color: white; text-decoration: none; border-radius: 4px; display: inline-block; font-weight: bold; margin-top: 20px; }
    </style>
</head>
<body>
<div class="receipt-card">
    <h2 style="color: #2cb67d; margin-top:0;">Congratulations!</h2>
    <p>Candidate Name: <strong><?php echo htmlspecialchars($print_name); ?></strong></p>
    <p>Matric Identification: <strong><?php echo htmlspecialchars($print_matric); ?></strong></p>
    <p>Exam Course Unit: <strong><?php echo htmlspecialchars($print_course); ?></strong></p>
    <hr style="border:0; border-top:1px dashed #ddd; margin:20px 0;">
  <!--  <div class="metric"><?php // echo $score; ?> / <?php // echo $total; ?></div>
    <p>Final Score Rank: <strong><?php //echo number_format($percentage,2); ?>%</strong></p>
    <div class="badge <?php //echo ($status === 'Passed') ? 'pass' : 'fail'; ?>"><?php // echo $status; ?></div>
    <br><br> -->
    <a href="portal.php" class="btn">Return to Portal Home</a>
</div>
</body>
</html>
