<?php
require_once __DIR__ . '/../config/database.php';

requireLogin();
requireAnyRole(['instructor']);

$user = getCurrentUser();
if (!$user) {
    redirect('login.php');
}

$db = Database::getInstance();

$cohorts = $db->getAll(
    "SELECT coh.*, c.title AS course_title,
            (SELECT COUNT(*) FROM course_enrollments ce WHERE ce.cohort_id = coh.id) AS enrolled_count
       FROM cohorts coh
       JOIN courses c ON coh.course_id = c.id
      WHERE coh.instructor_id = ?
   ORDER BY coh.start_date IS NULL, coh.start_date ASC, coh.created_at DESC",
    [$user['id']]
);

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

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h4 mb-0">Cohorts</h1>
        <p class="text-muted mb-0">Manage the cohorts you are leading.</p>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <?php if (empty($cohorts)): ?>
            <p class="text-muted mb-0">No cohorts found. Ask an admin to create cohorts for your courses.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-striped align-middle">
                    <thead>
                        <tr>
                            <th>Course</th>
                            <th>Title</th>
                            <th>Region</th>
                            <th>Start</th>
                            <th>End</th>
                            <th>Status</th>
                            <th>Enrollment</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($cohorts as $row): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($row['course_title']); ?></td>
                                <td><?php echo htmlspecialchars($row['title'] ?: 'Cohort #' . (int)$row['id']); ?></td>
                                <td><?php echo htmlspecialchars(strtoupper($row['region'])); ?></td>
                                <td><?php echo $row['start_date'] ? htmlspecialchars(date('M j, Y', strtotime($row['start_date']))) : 'TBA'; ?></td>
                                <td><?php echo $row['end_date'] ? htmlspecialchars(date('M j, Y', strtotime($row['end_date']))) : 'TBA'; ?></td>
                                <td>
                                    <span class="badge bg-label-<?php echo $row['status'] === 'open' ? 'success' : ($row['status'] === 'ongoing' ? 'primary' : 'secondary'); ?>">
                                        <?php echo htmlspecialchars(ucfirst($row['status'])); ?>
                                    </span>
                                </td>
                                <td>
                                    <?php
                                    $max = isset($row['max_capacity']) && (int)$row['max_capacity'] > 0 ? (int)$row['max_capacity'] : null;
                                    $enrolled = (int)($row['enrolled_count'] ?? 0);
                                    echo $max ? ($enrolled . ' / ' . $max) : $enrolled;
                                    ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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