<?php
require_once __DIR__ . '/../config/database.php';
requireLogin();
if (!hasRole('instructor') && !hasPermission('can_view_reports')) redirect('dashboard.php');
$db = Database::getInstance();
$user = getCurrentUser();

// If instructor, show only their assessments
$assessments = $db->getAll("SELECT a.* FROM assessments a WHERE a.created_by = ? ORDER BY a.created_at DESC", [$user['id']]);

$rows = [];
foreach ($assessments as $a) {
    $stats = $db->get("SELECT COUNT(*) as attempts, AVG(score) as avg_score, SUM(CASE WHEN passed = 1 THEN 1 ELSE 0 END) as passed_count FROM assessment_submissions WHERE assessment_id = ?", [$a['id']]);
    $attempts = (int)($stats['attempts'] ?? 0);
    $avg = round((float)($stats['avg_score'] ?? 0), 2);
    $passed = (int)($stats['passed_count'] ?? 0);
    $pass_rate = $attempts > 0 ? round(($passed / $attempts) * 100, 2) : null;
    $rows[] = [ 'assessment' => $a['title'], 'attempts' => $attempts, 'avg' => $avg, 'pass_rate' => $pass_rate, 'id' => $a['id'] ];
}

$pageTitle = 'Assessment Reports';
include __DIR__ . '/includes/header.php';
?>

<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-header d-flex align-items-center justify-content-between">
                <h5 class="m-0">Assessment Reports</h5>
            </div>
            <div class="card-body">
                <?php if (empty($rows)): ?>
                    <p class="text-muted">No assessments found.</p>
                <?php else: ?>
                    <table class="table table-striped">
                        <thead>
                            <tr><th>Assessment</th><th>Attempts</th><th>Avg Score</th><th>Pass Rate</th><th></th></tr>
                        </thead>
                        <tbody>
                            <?php foreach ($rows as $r): ?>
                                <tr>
                                    <td><?php echo htmlspecialchars($r['assessment']); ?></td>
                                    <td><?php echo (int)$r['attempts']; ?></td>
                                    <td><?php echo is_null($r['avg']) ? '-' : htmlspecialchars($r['avg']); ?></td>
                                    <td><?php echo is_null($r['pass_rate']) ? '-' : htmlspecialchars($r['pass_rate']) . '%'; ?></td>
                                    <td>
                                        <a class="btn btn-sm btn-outline-secondary" href="<?php echo APP_URL; ?>/lms/assessments_view.php?id=<?php echo (int)$r['id']; ?>">View</a>
                                        <a class="btn btn-sm btn-outline-primary ms-2" href="<?php echo APP_URL; ?>/lms/assessments_report_detail.php?id=<?php echo (int)$r['id']; ?>">Attempts</a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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