<?php
session_start();
require_once('../config.php');

if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'lecturer') {
    header("Location: dashboard.php");
    exit();
}

include('../header/header.php');

$lecturer_id = $_SESSION['user_id'];
$success = '';
$error = '';

// Handle assignment creation
if (isset($_POST['create_assignment'])) {
    $course_id = $_POST['course_id'];
    $title = $_POST['title'];
    $description = $_POST['description'];
    $due_date = $_POST['due_date'];

    try {
        $stmt = $pdo->prepare("INSERT INTO assignments (course_id, title, description, due_date) VALUES (?, ?, ?, ?)");
        $stmt->execute([$course_id, $title, $description, $due_date]);
        $success = "Assignment created successfully!";
    } catch (PDOException $e) {
        $error = "Error creating assignment.";
    }
}

// Fetch courses managed by this lecturer
$stmt = $pdo->prepare("SELECT id, course_name FROM courses WHERE lecturer_id = ?");
$stmt->execute([$lecturer_id]);
$courses = $stmt->fetchAll();

// Fetch assignments created by this lecturer
$stmt = $pdo->prepare("
    SELECT a.*, c.course_name, (SELECT COUNT(*) FROM submissions WHERE assignment_id = a.id AND status = 'submitted') as pending_count
    FROM assignments a
    JOIN courses c ON a.course_id = c.id
    WHERE c.lecturer_id = ?
    ORDER BY a.created_at DESC
");
$stmt->execute([$lecturer_id]);
$assignments = $stmt->fetchAll();
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold">Manage Assignments</h2>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createModal">
            <i class="fas fa-plus me-2"></i>Create Assignment
        </button>
    </div>

    <?php if($success): ?>
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <?php echo $success; ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm border-0">
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="bg-light">
                                <tr>
                                    <th class="ps-4">Assignment Title</th>
                                    <th>Course</th>
                                    <th>Due Date</th>
                                    <th>Pending Submissions</th>
                                    <th class="text-end pe-4">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (empty($assignments)): ?>
                                    <tr><td colspan="5" class="text-center py-5 text-muted">No assignments created yet.</td></tr>
                                <?php endif; ?>
                                <?php foreach($assignments as $a): ?>
                                    <tr>
                                        <td class="ps-4 fw-bold"><?php echo htmlspecialchars($a['title']); ?></td>
                                        <td><?php echo htmlspecialchars($a['course_name']); ?></td>
                                        <td><?php echo date('M d, Y H:i', strtotime($a['due_date'])); ?></td>
                                        <td>
                                            <?php if ($a['pending_count'] > 0): ?>
                                                <span class="badge bg-danger"><?php echo $a['pending_count']; ?> New</span>
                                            <?php else: ?>
                                                <span class="badge bg-secondary">None</span>
                                            <?php endif; ?>
                                        </td>
                                        <td class="text-end pe-4">
                                            <a href="grade_assignments.php?id=<?php echo $a['id']; ?>" class="btn btn-sm btn-outline-primary">
                                                <i class="fas fa-graduation-cap me-1"></i> Grade Submissions
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Create Modal -->
<div class="modal fade" id="createModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content border-0 shadow">
            <div class="modal-header">
                <h5 class="modal-title fw-bold">New Assignment</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label small fw-bold">Target Course</label>
                        <select name="course_id" class="form-select" required>
                            <?php foreach($courses as $c): ?>
                                <option value="<?php echo $c['id']; ?>"><?php echo htmlspecialchars($c['course_name']); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-bold">Assignment Title</label>
                        <input type="text" name="title" class="form-control" placeholder="e.g. Final Project" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-bold">Instructions</label>
                        <textarea name="description" class="form-control" rows="4" placeholder="What should students do?" required></textarea>
                    </div>
                    <div class="mb-3">
                        <label class="form-label small fw-bold">Due Date & Time</label>
                        <input type="datetime-local" name="due_date" class="form-control" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" name="create_assignment" class="btn btn-primary px-4">Create</button>
                </div>
            </form>
        </div>
    </div>
</div>

<?php include('../footer/footer.php'); ?>