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

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

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

$user_id = $_SESSION['user_id'];

$stmt = $pdo->prepare("SELECT * FROM courses WHERE lecturer_id = ? ORDER BY created_at DESC");
$stmt->execute([$user_id]);
$courses = $stmt->fetchAll();

$course_data = [];
foreach ($courses as $course) {
    $course_id = $course['id'];
    
    $stmt = $pdo->prepare("SELECT * FROM modules WHERE course_id = ? ORDER BY id");
    $stmt->execute([$course_id]);
    $modules = $stmt->fetchAll();
    
    $modules_with_units = [];
    foreach ($modules as $module) {
        $module_id = $module['id'];
        
        $stmt = $pdo->prepare("SELECT * FROM units WHERE module_id = ? ORDER BY id");
        $stmt->execute([$module_id]);
        $units = $stmt->fetchAll();
        
        $units_with_notes = [];
        foreach ($units as $unit) {
            $unit_id = $unit['id'];
            
            $stmt = $pdo->prepare("SELECT * FROM notes WHERE unit_id = ? ORDER BY uploaded_at DESC");
            $stmt->execute([$unit_id]);
            $notes = $stmt->fetchAll();
            
            $units_with_notes[] = [
                'unit' => $unit,
                'notes' => $notes
            ];
        }
        
        $modules_with_units[] = [
            'module' => $module,
            'units' => $units_with_notes
        ];
    }
    
    $course_data[] = [
        'course' => $course,
        'modules' => $modules_with_units
    ];
}
?>

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold">My Courses</h2>
        <div class="d-flex gap-2">
            <a href="add_course.php" class="btn btn-primary"><i class="fas fa-plus me-2"></i>New Course</a>
            <a href="dashboard.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left me-2"></i>Back</a>
        </div>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body">
            <?php if (empty($course_data)): ?>
                <div class="text-center py-5">
                    <i class="fas fa-book-open fa-3x text-muted mb-3"></i>
                    <h4 class="text-muted">No courses yet</h4>
                    <p class="text-muted mb-4">Start by creating your first course!</p>
                    <a href="add_course.php" class="btn btn-primary"><i class="fas fa-plus me-2"></i>Create Course</a>
                </div>
            <?php else: ?>
                <?php foreach ($course_data as $cd): ?>
                    <div class="mb-5 border-bottom pb-4">
                        <div class="d-flex justify-content-between align-items-start mb-3">
                            <div>
                                <h3 class="fw-bold mb-1"><?= htmlspecialchars($cd['course']['course_name']); ?></h3>
                                <span class="badge bg-secondary"><?= count($cd['modules']); ?> Module<?= count($cd['modules']) != 1 ? 's' : '' ?></span>
                                <?php if (!empty($cd['course']['category'])): ?>
                                    <span class="badge bg-info ms-2"><?= htmlspecialchars($cd['course']['category']); ?></span>
                                <?php endif; ?>
                            </div>
                            <div class="d-flex gap-2">
                                <a href="add_module.php?course_id=<?= $cd['course']['id']; ?>" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-plus"></i> Module
                                </a>
                            </div>
                        </div>
                        
                        <?php if (!empty($cd['course']['description'])): ?>
                            <p class="text-muted mb-4"><?= htmlspecialchars($cd['course']['description']); ?></p>
                        <?php endif; ?>

                        <?php if (empty($cd['modules'])): ?>
                            <div class="alert alert-info">
                                <i class="fas fa-info-circle me-2"></i>No modules yet for this course. <a href="add_module.php?course_id=<?= $cd['course']['id']; ?>" class="alert-link">Add a module</a>
                            </div>
                        <?php else: ?>
                            <div class="accordion" id="course-<?= $cd['course']['id']; ?>">
                                <?php foreach ($cd['modules'] as $mid => $mwd): ?>
                                    <div class="accordion-item">
                                        <h2 class="accordion-header" id="heading-<?= $cd['course']['id']; ?>-<?= $mid; ?>">
                                            <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapse-<?= $cd['course']['id']; ?>-<?= $mid; ?>" aria-expanded="true" aria-controls="collapse-<?= $cd['course']['id']; ?>-<?= $mid; ?>">
                                                <div class="d-flex justify-content-between align-items-center w-100 pe-3">
                                                    <span>
                                                        <i class="fas fa-folder me-2 text-warning"></i>
                                                        <strong><?= htmlspecialchars($mwd['module']['module_name']); ?></strong>
                                                    </span>
                                                    <span class="badge bg-light text-dark">
                                                        <?= count($mwd['units']); ?> Unit<?= count($mwd['units']) != 1 ? 's' : '' ?>
                                                    </span>
                                                </div>
                                            </button>
                                        </h2>
                                        <div id="collapse-<?= $cd['course']['id']; ?>-<?= $mid; ?>" class="accordion-collapse collapse show" aria-labelledby="heading-<?= $cd['course']['id']; ?>-<?= $mid; ?>" data-bs-parent="#course-<?= $cd['course']['id']; ?>">
                                            <div class="accordion-body">
                                                <div class="d-flex justify-content-end mb-3">
                                                    <a href="add_unit.php?module_id=<?= $mwd['module']['id']; ?>" class="btn btn-sm btn-outline-success">
                                                        <i class="fas fa-plus"></i> Unit
                                                    </a>
                                                </div>
                                                
                                                <?php if (empty($mwd['units'])): ?>
                                                    <div class="alert alert-info">
                                                        <i class="fas fa-info-circle me-2"></i>No units yet. <a href="add_unit.php?module_id=<?= $mwd['module']['id']; ?>" class="alert-link">Add a unit</a>
                                                    </div>
                                                <?php else: ?>
                                                    <div class="table-responsive">
                                                        <table class="table table-hover align-middle">
                                                            <thead class="bg-light">
                                                                <tr>
                                                                    <th>Unit</th>
                                                                    <th>Notes</th>
                                                                    <th class="text-end">Actions</th>
                                                                </tr>
                                                            </thead>
                                                            <tbody>
                                                                <?php foreach ($mwd['units'] as $uwm): ?>
                                                                    <tr>
                                                                        <td>
                                                                            <i class="fas fa-file-alt me-2 text-primary"></i>
                                                                            <?= htmlspecialchars($uwm['unit']['unit_name']); ?>
                                                                        </td>
                                                                        <td>
                                                                            <?php if (empty($uwm['notes'])): ?>
                                                                                <span class="text-muted small">No notes</span>
                                                                            <?php else: ?>
                                                                                <span class="badge bg-success"><?= count($uwm['notes']); ?> Note<?= count($uwm['notes']) != 1 ? 's' : '' ?></span>
                                                                            <?php endif; ?>
                                                                        </td>
                                                                        <td class="text-end">
                                                                            <div class="btn-group">
                                                                                <?php if (!empty($uwm['notes'])): ?>
                                                                                    <div class="dropdown">
                                                                                        <button class="btn btn-sm btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown">
                                                                                            <i class="fas fa-download"></i>
                                                                                        </button>
                                                                                        <ul class="dropdown-menu dropdown-menu-end">
                                                                                            <?php foreach ($uwm['notes'] as $note): ?>
                                                                                                <li>
                                                                                                    <a class="dropdown-item" href="../uploads/<?= $note['file_path']; ?>" download>
                                                                                                        <i class="fas fa-file-download me-2"></i>
                                                                                                        <?= htmlspecialchars($note['file_name']); ?>
                                                                                                    </a>
                                                                                                </li>
                                                                                            <?php endforeach; ?>
                                                                                        </ul>
                                                                                    </div>
                                                                                <?php endif; ?>
                                                                                <a href="upload_notes.php?course_id=<?= $cd['course']['id']; ?>&module_id=<?= $mwd['module']['id']; ?>&unit_id=<?= $uwm['unit']['id']; ?>" class="btn btn-sm btn-outline-success">
                                                                                    <i class="fas fa-upload"></i>
                                                                                </a>
                                                                            </div>
                                                                        </td>
                                                                    </tr>
                                                                <?php endforeach; ?>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
        </div>
    </div>
</div>

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