<?php
session_start();
require_once "inc/connect.php";

$token = $_GET['token'] ?? '';
$attempts = [];
$student = null;

if ($token !== '') {

    /* ===============================
       STUDENT / EXAM SUMMARY
    =============================== */

    $summaryQuery = "
        SELECT

            s.serial AS student_serial,

            CONCAT(
                s.surname,' ',
                s.middlename,' ',
                s.lastname
            ) AS full_name,

            s.school_id,

            tm.token_id,

            tm.examination_id,

            et.examination_title,

            tm.login_time,
            tm.last_activity,
            tm.status AS token_status

        FROM token_management tm

        LEFT JOIN student_table s
            ON s.serial = tm.student_id

        LEFT JOIN examination_table et
            ON et.examination_id = tm.examination_id

        WHERE tm.token_id = ?
        LIMIT 1
    ";

    $stmt = $mysqli->prepare($summaryQuery);
    $stmt->bind_param("s", $token);
    $stmt->execute();

    $student = $stmt->get_result()->fetch_assoc();



    /* ===============================
       ATTEMPT LOG ANALYSIS
    =============================== */

    $query = "
        SELECT

            sal.serial,

            sal.question_set_ref,

            sal.question_id,

            sal.submitted_answer,

            sal.correct_answer,

            sal.loading_time,

            sal.time_of_submission,

            sal.submitted_status,

            sal.score,

            sal.status,

            qs.question_number,

            qs.first_attempt,

            qs.second_attempt,

            qs.allow_repeat,

            qs.question_option_sequence,

            TIMESTAMPDIFF(
                SECOND,
                sal.loading_time,
                sal.time_of_submission
            ) AS time_spent,

            CASE
                WHEN sal.score > 0
                THEN 'Correct'
                ELSE 'Wrong'
            END AS result_state,

            CASE
                WHEN qs.second_attempt IS NOT NULL
                THEN 'Repeated'
                ELSE 'Single Attempt'
            END AS repeat_state,

            CASE
                WHEN TIMESTAMPDIFF(
                    SECOND,
                    sal.loading_time,
                    sal.time_of_submission
                ) <= 5
                THEN 'Suspiciously Fast'

                WHEN TIMESTAMPDIFF(
                    SECOND,
                    sal.loading_time,
                    sal.time_of_submission
                ) <= 15
                THEN 'Fast'

                WHEN TIMESTAMPDIFF(
                    SECOND,
                    sal.loading_time,
                    sal.time_of_submission
                ) <= 60
                THEN 'Normal'

                ELSE 'Slow / Careful'
            END AS speed_state

        FROM student_attempt_log sal

        LEFT JOIN question_set qs
            ON qs.serial = sal.question_set_ref

        WHERE sal.token_id = ?

        ORDER BY sal.loading_time ASC
    ";

    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("s", $token);
    $stmt->execute();

    $attempts = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Exam Attempt Timeline</title>

    <link rel="stylesheet" href="/plugins/fontawesome-free/css/all.min.css">
    <link rel="stylesheet" href="/dist/css/adminlte.min.css">

    <style>

        .timeline-item{
            border-left:4px solid #007bff;
            margin-bottom:20px;
            padding-left:20px;
            position:relative;
        }

        .timeline-item::before{
            content:'';
            width:14px;
            height:14px;
            background:#007bff;
            border-radius:50%;
            position:absolute;
            left:-9px;
            top:5px;
        }

        .metric{
            font-size:13px;
            margin-bottom:5px;
        }

        .metric strong{
            display:inline-block;
            width:170px;
        }

        .answer-box{
            padding:10px;
            border-radius:5px;
            background:#f8f9fa;
            margin-top:10px;
        }

    </style>

</head>

<body class="hold-transition sidebar-mini layout-fixed">

<div class="wrapper">

<?php include 'inc/admin_nav.php'; ?>

<div class="content-wrapper">

    <div class="content-header">
        <div class="container-fluid">

            <h1 class="m-0">
                Exam Attempt Timeline
            </h1>

        </div>
    </div>

    <div class="content">

        <div class="container-fluid">

            <!-- SEARCH -->

            <div class="card">

                <div class="card-body">

                    <form method="get">

                        <div class="input-group">

                            <input
                                type="text"
                                name="token"
                                class="form-control"
                                placeholder="Enter Token ID"
                                value="<?= htmlspecialchars($token) ?>"
                            >

                            <div class="input-group-append">
                                <button class="btn btn-primary">
                                    <i class="fas fa-search"></i>
                                    Load Attempt
                                </button>
                            </div>

                        </div>

                    </form>

                </div>

            </div>

            <?php if($student): ?>

            <!-- STUDENT INFO -->

            <div class="card card-primary">

                <div class="card-header">
                    <h3 class="card-title">
                        Student Examination Information
                    </h3>
                </div>

                <div class="card-body">

                    <div class="row">

                        <div class="col-md-4">
                            <strong>Student:</strong><br>
                            <?= htmlspecialchars($student['full_name']) ?>
                        </div>

                        <div class="col-md-4">
                            <strong>Student ID:</strong><br>
                            <?= htmlspecialchars($student['school_id']) ?>
                        </div>

                        <div class="col-md-4">
                            <strong>Exam:</strong><br>
                            <?= htmlspecialchars($student['examination_title']) ?>
                        </div>

                    </div>

                    <hr>

                    <div class="row">

                        <div class="col-md-3">
                            <strong>Token:</strong><br>
                            <?= htmlspecialchars($student['token_id']) ?>
                        </div>

                        <div class="col-md-3">
                            <strong>Login Time:</strong><br>
                            <?= $student['login_time'] ?>
                        </div>

                        <div class="col-md-3">
                            <strong>Last Activity:</strong><br>
                            <?= $student['last_activity'] ?>
                        </div>

                        <div class="col-md-3">
                            <strong>Status:</strong><br>

                            <?php
                            if($student['token_status']=='used'){
                                echo '<span class="badge badge-success">USED</span>';
                            }else{
                                echo '<span class="badge badge-warning">'.$student['token_status'].'</span>';
                            }
                            ?>

                        </div>

                    </div>

                </div>

            </div>

            <!-- TIMELINE -->

            <div class="card">

                <div class="card-header">
                    <h3 class="card-title">
                        Chronological Examination Activity
                    </h3>
                </div>

                <div class="card-body">

                    <?php foreach($attempts as $a): ?>

                    <div class="timeline-item">

                        <h5>

                            Question <?= $a['question_number'] ?>

                            <?php
                            if($a['score'] > 0){
                                echo '<span class="badge badge-success ml-2">Correct</span>';
                            }else{
                                echo '<span class="badge badge-danger ml-2">Wrong</span>';
                            }
                            ?>

                            <?php
                            if($a['second_attempt']){
                                echo '<span class="badge badge-warning ml-2">Repeated</span>';
                            }
                            ?>

                        </h5>

                        <div class="metric">
                            <strong>Question ID:</strong>
                            <?= $a['question_id'] ?>
                        </div>

                        <div class="metric">
                            <strong>Question Loaded:</strong>
                            <?= $a['loading_time'] ?>
                        </div>

                        <div class="metric">
                            <strong>Submission Time:</strong>
                            <?= $a['time_of_submission'] ?>
                        </div>

                        <div class="metric">
                            <strong>Time Spent:</strong>

                            <?php

                            $timeSpent = (int)$a['time_spent'];

                            if($timeSpent <= 5){
                                echo '<span class="badge badge-danger">';
                            }elseif($timeSpent <= 15){
                                echo '<span class="badge badge-warning">';
                            }else{
                                echo '<span class="badge badge-info">';
                            }

                            echo $timeSpent . ' sec</span>';
                            ?>

                        </div>

                        <div class="metric">
                            <strong>Speed Behaviour:</strong>

                            <?php
                            echo $a['speed_state'];
                            ?>
                        </div>

                        <div class="metric">
                            <strong>Repeat Behaviour:</strong>
                            <?= $a['repeat_state'] ?>
                        </div>

                        <div class="metric">
                            <strong>Submitted Status:</strong>

                            <?php
                            if($a['submitted_status']=='submitted'){
                                echo '<span class="badge badge-success">Submitted</span>';
                            }elseif($a['submitted_status']=='skipped'){
                                echo '<span class="badge badge-warning">Skipped</span>';
                            }else{
                                echo '<span class="badge badge-secondary">Pending</span>';
                            }
                            ?>

                        </div>

                        <div class="metric">
                            <strong>Score:</strong>

                            <?php
                            if($a['score'] > 0){
                                echo '<span class="badge badge-success">';
                            }else{
                                echo '<span class="badge badge-danger">';
                            }

                            echo $a['score'];
                            echo '</span>';
                            ?>

                        </div>

                        <div class="answer-box">

                            <div class="metric">
                                <strong>Submitted Answer:</strong>
                                <?= htmlspecialchars($a['submitted_answer']) ?>
                            </div>

                            <div class="metric">
                                <strong>Correct Answer:</strong>
                                <?= htmlspecialchars($a['correct_answer']) ?>
                            </div>

                            <div class="metric">
                                <strong>Option Sequence:</strong>
                                <?= htmlspecialchars($a['question_option_sequence']) ?>
                            </div>

                        </div>

                    </div>

                    <?php endforeach; ?>

                </div>

            </div>

            <?php elseif($token): ?>

                <div class="alert alert-danger">
                    No attempt record found for this token.
                </div>

            <?php endif; ?>

        </div>

    </div>

</div>

</div>

<script src="/plugins/jquery/jquery.min.js"></script>
<script src="/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="/dist/js/adminlte.min.js"></script>

</body>
</html>