<?php
require_once __DIR__ . '/../config/database.php';
requireLogin();
$db = Database::getInstance();
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$assessment = $db->get("SELECT a.*, u.first_name, u.last_name FROM assessments a LEFT JOIN users u ON a.created_by = u.id WHERE a.id = ?", [$id]);
if (!$assessment) {
    header('Location: ' . APP_URL . '/lms/assessments.php');
    exit;
}

$pageTitle = 'Assessment - ' . htmlspecialchars($assessment['title']);
include __DIR__ . '/includes/header.php';
?>
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="m-0"><?php echo htmlspecialchars($assessment['title']); ?></h5>
                <small class="text-muted"><?php echo htmlspecialchars($assessment['type']); ?> &middot; Created by <?php echo htmlspecialchars(trim($assessment['first_name'] . ' ' . $assessment['last_name'])); ?></small>
            </div>

            <div class="card-body">
                <p><?php echo nl2br(htmlspecialchars($assessment['description'])); ?></p>

                <p><strong>Time limit:</strong> <?php echo $assessment['time_limit_minutes'] ? (int)$assessment['time_limit_minutes'] . ' minutes' : 'No limit'; ?></p>

                <?php if ($assessment['is_published'] || hasRole('instructor')): ?>
                    <a href="<?php echo APP_URL; ?>/lms/assessments_take.php?id=<?php echo (int)$assessment['id']; ?>" class="btn btn-primary">Start</a>
                    <?php if (hasRole('instructor')): ?>
                        <a href="<?php echo APP_URL; ?>/lms/assessment_questions.php?assessment_id=<?php echo (int)$assessment['id']; ?>" class="btn btn-outline-secondary ms-2">Manage Questions</a>
                    <?php endif; ?>
                <?php else: ?>
                    <p class="text-muted">This assessment is not available.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php include __DIR__ . '/includes/footer.php';
