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

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

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

$db = Database::getInstance();
$instructorId = (int)($user['id'] ?? 0);

$stats = [
  'courses' => 0,
  'assessments' => 0,
  'active_students' => 0,
  'completions' => 0,
];

// Courses owned by instructor
$row = $db->get("SELECT COUNT(*) AS cnt FROM courses WHERE instructor_id = ?", [$instructorId]);
$stats['courses'] = (int)($row['cnt'] ?? 0);

// Assessments created by instructor (fallback to course-linked)
$row = $db->get("SELECT COUNT(*) AS cnt FROM assessments WHERE created_by = ?", [$instructorId]);
$stats['assessments'] = (int)($row['cnt'] ?? 0);

// Active students across instructor courses
$row = $db->get(
  "SELECT COUNT(DISTINCT ce.student_id) AS cnt
   FROM course_enrollments ce
   JOIN courses c ON ce.course_id = c.id
   WHERE c.instructor_id = ? AND ce.status IN ('active','completed')",
  [$instructorId]
);
$stats['active_students'] = (int)($row['cnt'] ?? 0);

// Completions across instructor courses
$row = $db->get(
  "SELECT COUNT(*) AS cnt
   FROM course_enrollments ce
   JOIN courses c ON ce.course_id = c.id
   WHERE c.instructor_id = ? AND ce.status = 'completed'",
  [$instructorId]
);
$stats['completions'] = (int)($row['cnt'] ?? 0);

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

<div class="row gy-6">
  <div class="col-12">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <div>
          <h5 class="card-title mb-0">Welcome, <?php echo htmlspecialchars($user['first_name'] ?? 'Instructor'); ?></h5>
          <small class="text-body-secondary">Create and manage your courses and assessments</small>
        </div>
        <a href="../courses.php" class="btn btn-primary btn-sm">New Course</a>
      </div>
      <div class="card-body">
        <div class="row g-6">
          <div class="col-sm-6 col-lg-3">
            <div class="d-flex align-items-center">
              <div class="avatar">
                <div class="avatar-initial bg-primary rounded shadow-xs"><i class="icon-base ri ri-book-2-line icon-24px"></i></div>
              </div>
              <div class="ms-3">
                <p class="mb-0">My Courses</p>
                <h5 class="mb-0"><?php echo number_format($stats['courses']); ?></h5>
              </div>
            </div>
          </div>
          <div class="col-sm-6 col-lg-3">
            <div class="d-flex align-items-center">
              <div class="avatar">
                <div class="avatar-initial bg-success rounded shadow-xs"><i class="icon-base ri ri-quill-pen-line icon-24px"></i></div>
              </div>
              <div class="ms-3">
                <p class="mb-0">Assessments</p>
                <h5 class="mb-0"><?php echo number_format($stats['assessments']); ?></h5>
              </div>
            </div>
          </div>
          <div class="col-sm-6 col-lg-3">
            <div class="d-flex align-items-center">
              <div class="avatar">
                <div class="avatar-initial bg-warning rounded shadow-xs"><i class="icon-base ri ri-team-line icon-24px"></i></div>
              </div>
              <div class="ms-3">
                <p class="mb-0">Active Students</p>
                <h5 class="mb-0"><?php echo number_format($stats['active_students']); ?></h5>
              </div>
            </div>
          </div>
          <div class="col-sm-6 col-lg-3">
            <div class="d-flex align-items-center">
              <div class="avatar">
                <div class="avatar-initial bg-info rounded shadow-xs"><i class="icon-base ri ri-trophy-line icon-24px"></i></div>
              </div>
              <div class="ms-3">
                <p class="mb-0">Completions</p>
                <h5 class="mb-0"><?php echo number_format($stats['completions']); ?></h5>
              </div>
            </div>
          </div>
        </div>
        <div class="mt-4">
          <a href="../courses.php" class="btn btn-outline-primary btn-sm me-2">Manage Courses</a>
          <a href="../dashboard.php" class="btn btn-outline-secondary btn-sm">Student Dashboard</a>
        </div>
      </div>
    </div>
  </div>
</div>

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