<?php 
// Debugging (Remove in production)
ini_set('display_errors', 1);
error_reporting(E_ALL);

session_start();

// 1. Database Connection
if (!file_exists('db_connect.php')) {
    die("Error: db_connect.php file is missing.");
}
require_once 'db_connect.php';

// 2. Security Check
if (!isset($_SESSION['student_id'])) { 
    header("Location: login.php"); 
    exit; 
}

// Check Result ID from URL
if(!isset($_GET['id'])) { 
    header("Location: live_exams.php"); 
    exit; 
}

$result_id = (int)$_GET['id'];
$student_id = $_SESSION['student_id'];

try {
    // Fetch detailed info by joining exam_result and exams tables
    $stmt = $pdo->prepare("
        SELECT r.*, e.exam_title, e.duration_minutes, e.negative_marks 
        FROM exam_result r 
        JOIN exams e ON r.exam_id = e.id 
        WHERE r.id = ? AND r.student_id = ?
    ");
    $stmt->execute([$result_id, $student_id]);
    $result = $stmt->fetch(PDO::FETCH_ASSOC);

    if(!$result) { 
        die("<div class='alert alert-danger m-5 text-center fw-bold shadow-sm'>Sorry, the result could not be found or it does not belong to you!</div>");
    }

} catch (PDOException $e) {
    die("Error: " . $e->getMessage());
}

// Include Header & Sidebar
if(file_exists('includes/user_header.php')) { include 'includes/user_header.php'; } 
elseif(file_exists('header.php')) { include 'header.php'; }

if(file_exists('includes/sidebar.php')) { include 'includes/sidebar.php'; } 
elseif(file_exists('sidebar.php')) { include 'sidebar.php'; }
?>

<style>
    :root {
        --primary: #064e3b;
        --primary-dark: #022c22;
        --accent: #d4af37; /* Golden */
    }

    .fade-up { animation: fadeInUp 0.6s ease-out forwards; }
    @keyframes fadeInUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
    
    .result-header { 
        background: linear-gradient(135deg, var(--primary-dark), var(--primary)); 
        position: relative; 
        overflow: hidden; 
    }
    .pattern-overlay { 
        position: absolute; 
        top: 0; left: 0; 
        width: 100%; height: 100%; 
        opacity: 0.1; 
        background-image: url('https://www.transparenttextures.com/patterns/cubes.png'); 
        pointer-events: none; 
    }
    
    .stat-box { transition: 0.3s; border: 2px solid transparent; }
    .stat-box:hover { transform: translateY(-5px); box-shadow: 0 10px 20px rgba(0,0,0,0.05); }
    
    .score-circle { 
        width: 120px; height: 120px; 
        background: #fff; 
        color: var(--primary-dark); 
        border-radius: 50%; 
        display: flex; align-items: center; justify-content: center; 
        font-size: 40px; font-weight: 900; 
        margin: 0 auto; 
        box-shadow: 0 10px 25px rgba(0,0,0,0.2); 
        border: 5px solid var(--accent); /* Golden Border */
    }
    
    .fw-800 { font-weight: 800; }
    .tracking-widest { letter-spacing: 0.1em; }
</style>

<div class="main-content p-4" style="background: #f8fafc; min-height: 80vh;">
    
    <div class="row justify-content-center fade-up">
        <div class="col-lg-8">
            <div class="card border-0 shadow-lg rounded-4 overflow-hidden mb-5">
                
                <div class="card-header result-header text-white text-center py-5 border-0">
                    <div class="pattern-overlay"></div>
                    <div style="position: relative; z-index: 1;">
                        <h6 class="text-uppercase tracking-widest opacity-75 mb-2">
                            <i class="fas fa-trophy me-2" style="color: var(--accent);"></i> Exam Result
                        </h6>
                        <h3 class="fw-800 mb-4"><?= htmlspecialchars($result['exam_title']); ?></h3>
                        
                        <div class="score-circle mb-3">
                            <?= number_format($result['score'], 2); ?>
                        </div>
                        <p class="fs-6 mb-0 opacity-75">Your Total Score</p>
                    </div>
                </div>
                
                <div class="card-body p-4 p-md-5 bg-white">
                    <div class="row g-3 text-center mb-5">
                        
                        <div class="col-6 col-md-3">
                            <div class="p-3 rounded-4 bg-light stat-box">
                                <h3 class="fw-800 mb-0 text-dark"><?= $result['total_questions']; ?></h3>
                                <small class="text-muted fw-bold">Total Questions</small>
                            </div>
                        </div>
                        
                        <div class="col-6 col-md-3">
                            <div class="p-3 border-success border-opacity-25 rounded-4 bg-success bg-opacity-10 stat-box border">
                                <h3 class="fw-800 text-success mb-0"><?= $result['correct_answers']; ?></h3>
                                <small class="text-success fw-bold">Correct</small>
                            </div>
                        </div>
                        
                        <div class="col-6 col-md-3">
                            <div class="p-3 border-danger border-opacity-25 rounded-4 bg-danger bg-opacity-10 stat-box border">
                                <h3 class="fw-800 text-danger mb-0"><?= $result['wrong_answers']; ?></h3>
                                <small class="text-danger fw-bold">Wrong</small>
                            </div>
                        </div>
                        
                        <div class="col-6 col-md-3">
                            <div class="p-3 rounded-4 stat-box border" style="background-color: rgba(212, 175, 55, 0.1); border-color: rgba(212, 175, 55, 0.3);">
                                <h3 class="fw-800 mb-0" style="color: #b48a1d;"><?= $result['not_attempted']; ?></h3>
                                <small class="fw-bold" style="color: #b48a1d;">Unattempted</small>
                            </div>
                        </div>
                    </div>

                    <div class="mb-5">
                        <div class="d-flex justify-content-between mb-2">
                            <h6 class="fw-bold text-dark mb-0">Success Rate</h6>
                            <h6 class="fw-bold text-dark mb-0"><?= $result['percentage']; ?>%</h6>
                        </div>
                        <div class="progress shadow-sm" style="height: 14px; border-radius: 50px; background: #e2e8f0;">
                            <?php 
                            $bar_color = ($result['percentage'] >= 40) ? 'bg-success' : 'bg-danger';
                            ?>
                            <div class="progress-bar <?= $bar_color; ?> progress-bar-striped progress-bar-animated" role="progressbar" style="width: <?= $result['percentage']; ?>%"></div>
                        </div>
                    </div>

                    <?php if($result['negative_marks'] > 0): ?>
                    <div class="alert rounded-4 p-4 mb-5" style="background: #f0fdf4; border: 1px solid #bbf7d0;">
                        <div class="d-flex align-items-center">
                            <i class="fas fa-info-circle fa-2x text-success me-3"></i>
                            <div>
                                <h6 class="fw-bold text-dark mb-1">Important Information:</h6>
                                <span class="small text-muted">In this exam, <strong><?= $result['negative_marks']; ?></strong> marks were deducted for each incorrect answer.</span>
                            </div>
                        </div>
                    </div>
                    <?php endif; ?>

                    <div class="d-flex flex-column flex-md-row gap-3">
                        <a href="review_exam.php?id=<?= $result_id; ?>" class="btn w-100 rounded-pill py-3 fw-bold shadow-sm" style="background-color: var(--accent); color: var(--primary-dark); border: none; transition: 0.3s;" onmouseover="this.style.transform='translateY(-2px)'" onmouseout="this.style.transform='translateY(0)'">
                            <i class="fas fa-search me-2"></i> Review Answer Sheet
                        </a>
                        <a href="exams.php" class="btn w-100 rounded-pill py-3 fw-bold shadow-sm" style="background-color: var(--primary); color: white; border: none; transition: 0.3s;" onmouseover="this.style.transform='translateY(-2px)'" onmouseout="this.style.transform='translateY(0)'">
                            Take New Exam <i class="fas fa-arrow-right ms-2"></i>
                        </a>
                    </div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

<?php 
// Include Footer
if(file_exists('includes/user_footer.php')) { include 'includes/user_footer.php'; } 
elseif(file_exists('footer.php')) { include 'footer.php'; }
?>