<?php
/**
 * LMS Assessments listing
 */
require_once __DIR__ . '/../config/database.php';
requireLogin();
$user = getCurrentUser();
$db = Database::getInstance();

// Filtering: by lesson_id or course_id
$lessonIdFilter = isset($_GET['lesson_id']) ? (int)$_GET['lesson_id'] : 0;
$courseIdFilter = isset($_GET['course_id']) ? (int)$_GET['course_id'] : 0;

$where = '';
$params = [];
if ($lessonIdFilter) {
    $where = ' WHERE a.lesson_id = ? ';
    $params = [$lessonIdFilter];
} elseif ($courseIdFilter) {
    $where = ' WHERE a.course_id = ? ';
    $params = [$courseIdFilter];
}

if (hasRole('instructor')) {
    if ($where) {
        $assessments = $db->getAll("SELECT a.*, u.first_name, u.last_name FROM assessments a LEFT JOIN users u ON a.created_by = u.id " . $where . " AND a.created_by = ? ORDER BY a.created_at DESC", array_merge($params, [$user['id']]));
    } else {
        $assessments = $db->getAll("SELECT a.*, u.first_name, u.last_name FROM assessments a LEFT JOIN users u ON a.created_by = u.id WHERE a.created_by = ? ORDER BY a.created_at DESC", [$user['id']]);
    }
} else {
    if ($where) {
        $assessments = $db->getAll("SELECT a.*, u.first_name, u.last_name FROM assessments a LEFT JOIN users u ON a.created_by = u.id " . $where . " AND a.is_published = 1 ORDER BY a.available_from DESC", array_merge($params));
    } else {
        $assessments = $db->getAll("SELECT a.*, u.first_name, u.last_name FROM assessments a LEFT JOIN users u ON a.created_by = u.id WHERE a.is_published = 1 ORDER BY a.available_from DESC", []);
    }
}

$pageTitle = 'Assessments';
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">Assessments</h5>
                <div>
                <?php if (hasRole('instructor') || hasPermission('can_create_assessments')): ?>
                <a href="<?php echo APP_URL; ?>/lms/assessments_edit.php" class="btn btn-primary btn-sm">New Assessment</a>
                <?php endif; ?>
                <?php if (hasRole('instructor') || hasPermission('can_view_reports')): ?>
                    <a href="<?php echo APP_URL; ?>/lms/assessments_report.php" class="btn btn-outline-secondary btn-sm ms-2">Reports</a>
                <?php endif; ?>
                </div>
            </div>

            <div class="card-body">
                <?php if (empty($assessments)): ?>
                    <p class="text-muted">No assessments found.</p>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Title</th>
                                    <th>Type</th>
                                    <th>Course/Lesson</th>
                                    <th>Created By</th>
                                    <th>Published</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($assessments as $a): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($a['title']); ?></td>
                                        <td><?php echo htmlspecialchars($a['type']); ?></td>
                                        <td><?php echo htmlspecialchars($a['course_id'] ? 'Course #' . (int)$a['course_id'] : ($a['lesson_id'] ? 'Lesson #' . (int)$a['lesson_id'] : '-')); ?></td>
                                        <td><?php echo htmlspecialchars(trim(($a['first_name'] ?? '') . ' ' . ($a['last_name'] ?? ''))); ?></td>
                                        <td><?php echo $a['is_published'] ? 'Yes' : 'No'; ?></td>
                                        <td class="text-end">
                                            <a href="<?php echo APP_URL; ?>/lms/assessments_view.php?id=<?php echo (int)$a['id']; ?>" class="btn btn-sm btn-outline-primary">View</a>
                                            <?php if (hasRole('instructor') && (int)$a['created_by'] === (int)$user['id']): ?>
                                                <a href="<?php echo APP_URL; ?>/lms/assessments_edit.php?id=<?php echo (int)$a['id']; ?>" class="btn btn-sm btn-outline-secondary">Edit</a>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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