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

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

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

$db = Database::getInstance();

$cohorts = $db->getAll(
    "SELECT ce.*, c.title AS course_title, c.slug AS course_slug,
            coh.title AS cohort_title,
            coh.start_date, coh.end_date, coh.status AS cohort_status,
            u.first_name AS instructor_first_name, u.last_name AS instructor_last_name,
            a.id AS agreement_id, a.signed_at
       FROM course_enrollments ce
       JOIN courses c ON ce.course_id = c.id
  LEFT JOIN course_enrollment_agreements a ON a.enrollment_id = ce.id AND a.status = 'signed'
  LEFT JOIN cohorts coh ON ce.cohort_id = coh.id
  LEFT JOIN users u ON coh.instructor_id = u.id
      WHERE ce.student_id = ?
   ORDER BY coh.start_date IS NULL, coh.start_date ASC, ce.enrollment_date DESC",
    [$user['id']]
);

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

<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <h1 class="h4 mb-0">My Cohorts</h1>
        <p class="text-muted mb-0">View the cohorts you are enrolled in and their schedules.</p>
    </div>
</div>

<div class="card">
    <div class="card-body">
        <?php if (empty($cohorts)): ?>
            <p class="text-muted mb-0">You are not enrolled in any cohorts yet. Browse courses to get started.</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-striped align-middle">
                    <thead>
                        <tr>
                            <th>Course</th>
                            <th>Cohort</th>
                            <th>Instructor</th>
                            <th>Start</th>
                            <th>End</th>
                            <th>Status</th>
                            <th>Agreement</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($cohorts as $row): ?>
                            <tr>
                                <td>
                                    <a href="<?php echo APP_URL; ?>/lms/courses.php?course_id=<?php echo (int)$row['course_id']; ?>">
                                        <?php echo htmlspecialchars($row['course_title']); ?>
                                    </a>
                                </td>
                                <td><?php echo htmlspecialchars($row['cohort_title'] ?: 'Default Cohort'); ?></td>
                                <td>
                                    <?php
                                    $iname = trim(($row['instructor_first_name'] ?? '') . ' ' . ($row['instructor_last_name'] ?? ''));
                                    echo $iname !== '' ? htmlspecialchars($iname) : 'TBA';
                                    ?>
                                </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['cohort_status'] === 'open' ? 'success' : ($row['cohort_status'] === 'ongoing' ? 'primary' : 'secondary'); ?>">
                                        <?php echo htmlspecialchars(ucfirst($row['cohort_status'] ?? 'active')); ?>
                                    </span>
                                </td>
                                <td>
                                    <?php if (!empty($row['agreement_id'])): ?>
                                        <a href="<?php echo APP_URL; ?>/lms/view_agreement.php?enrollment_id=<?php echo (int)$row['id']; ?>" class="btn btn-sm btn-outline-secondary">
                                            View Agreement
                                        </a>
                                    <?php else: ?>
                                        <span class="text-muted">Not signed</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

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