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

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
$lesson_id = isset($_GET['lesson_id']) ? (int)$_GET['lesson_id'] : 0;
$assessment = null;
if ($id) {
    $assessment = $db->get("SELECT * FROM assessments WHERE id = ? AND created_by = ?", [$id, $user['id']]);
    if (!$assessment) {
        redirect('assessments.php');
    }
    $lesson_id = $assessment['lesson_id'] ?? $lesson_id;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = trim($_POST['title'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $type = $_POST['type'] ?? 'quiz';
    $time_limit = $_POST['time_limit_minutes'] ? (int)$_POST['time_limit_minutes'] : null;
    $is_published = isset($_POST['is_published']) ? 1 : 0;

    if ($id) {
        $db->query("UPDATE assessments SET title = ?, description = ?, type = ?, time_limit_minutes = ?, is_published = ?, lesson_id = ? WHERE id = ? AND created_by = ?", [$title, $description, $type, $time_limit, $is_published, $lesson_id ?: null, $id, $user['id']]);
        header('Location: ' . APP_URL . '/lms/assessments.php');
        exit;
    } else {
        $db->query("INSERT INTO assessments (course_id, lesson_id, title, description, type, time_limit_minutes, is_published, created_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [null, $lesson_id ?: null, $title, $description, $type, $time_limit, $is_published, $user['id']]);
        header('Location: ' . APP_URL . '/lms/assessments.php');
        exit;
    }
}

$pageTitle = $id ? 'Edit Assessment' : 'New Assessment';
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($pageTitle); ?></h5>
            </div>

            <div class="card-body">
                <form method="post">
                    <div class="mb-3">
                        <label class="form-label">Title</label>
                        <input class="form-control" name="title" value="<?php echo htmlspecialchars($assessment['title'] ?? ''); ?>" required />
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Description</label>
                        <textarea class="form-control" name="description"><?php echo htmlspecialchars($assessment['description'] ?? ''); ?></textarea>
                    </div>

                    <input type="hidden" name="lesson_id" value="<?php echo (int)$lesson_id; ?>" />
                    <?php if (!empty($lesson_id)): ?>
                        <div class="mb-3"><small class="text-muted">This assessment is linked to lesson #<?php echo (int)$lesson_id; ?></small></div>
                    <?php endif; ?>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label class="form-label">Type</label>
                            <select name="type" class="form-select">
                                <option value="quiz" <?php echo (isset($assessment['type']) && $assessment['type'] === 'quiz') ? 'selected' : ''; ?>>Quiz</option>
                                <option value="exam" <?php echo (isset($assessment['type']) && $assessment['type'] === 'exam') ? 'selected' : ''; ?>>Exam</option>
                                <option value="assignment" <?php echo (isset($assessment['type']) && $assessment['type'] === 'assignment') ? 'selected' : ''; ?>>Assignment</option>
                            </select>
                        </div>

                        <div class="col-md-4 mb-3">
                            <label class="form-label">Time limit (minutes)</label>
                            <input type="number" class="form-control" name="time_limit_minutes" value="<?php echo htmlspecialchars($assessment['time_limit_minutes'] ?? ''); ?>" />
                        </div>

                        <div class="col-md-4 mb-3 d-flex align-items-center">
                            <div class="form-check mt-2">
                                <input class="form-check-input" type="checkbox" name="is_published" id="is_published" <?php echo (!empty($assessment['is_published'])) ? 'checked' : ''; ?> />
                                <label class="form-check-label" for="is_published">Published</label>
                            </div>
                        </div>
                    </div>

                    <div class="mt-3">
                        <button class="btn btn-success">Save</button>
                        <a href="<?php echo APP_URL; ?>/lms/assessments.php" class="btn btn-outline-secondary ms-2">Cancel</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

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