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

requireLogin();
requireAnyRole(['admin', 'super_admin']);

$db = Database::getInstance();

$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
if ($id <= 0) {
    $_SESSION['flash']['danger'] = 'Invalid course ID.';
    header('Location: courses.php');
    exit;
}

$course = $db->get(
    "SELECT c.*, cat.name AS category_name, u.first_name, u.last_name
     FROM courses c
     LEFT JOIN categories cat ON c.category_id = cat.id
     LEFT JOIN users u ON c.instructor_id = u.id
     WHERE c.id = ?",
    [$id]
);

if (!$course) {
    $_SESSION['flash']['danger'] = 'Course not found.';
    header('Location: courses.php');
    exit;
}

$enrollments = $db->get(
    "SELECT COUNT(*) AS cnt, AVG(progress_percentage) AS avg_prog
     FROM course_enrollments
     WHERE course_id = ?",
    [$id]
);

$modules = $db->getAll(
    "SELECT m.*, COUNT(l.id) AS lesson_count
     FROM course_modules m
     LEFT JOIN course_lessons l ON m.id = l.module_id
     WHERE m.course_id = ?
     GROUP BY m.id
     ORDER BY m.sort_order",
    [$id]
);

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

<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="h4 mb-0">Course Details</h1>
    <p class="text-muted mb-0">View course information and content</p>
  </div>
  <div>
    <a href="courses_edit.php?id=<?php echo $id; ?>" class="btn btn-primary me-2">
      <i class="fas fa-edit me-2"></i>Edit
    </a>
    <a href="courses.php" class="btn btn-outline-secondary">
      <i class="fas fa-arrow-left me-2"></i>Back to Courses
    </a>
  </div>
</div>

<div class="row">
  <div class="col-md-8">
    <div class="card">
      <div class="card-header">
        <h5 class="mb-0"><?php echo htmlspecialchars($course['title'] ?? ''); ?></h5>
        <small class="text-muted">Slug: <?php echo htmlspecialchars($course['slug'] ?? ''); ?></small>
      </div>
      <div class="card-body">
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Instructor</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars(($course['first_name'] ?? '') . ' ' . ($course['last_name'] ?? '')); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Category</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars($course['category_name'] ?? ''); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Level</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars(ucfirst($course['level'] ?? '')); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Price</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars($course['currency'] ?? ''); ?> <?php echo number_format((float)($course['price'] ?? 0), 2); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Status</strong></div>
          <div class="col-sm-9">
            <span class="badge bg-<?php echo $course['status']==='published'?'success':($course['status']==='draft'?'warning':'secondary'); ?>">
              <?php echo htmlspecialchars(ucfirst($course['status'] ?? '')); ?>
            </span>
          </div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Created</strong></div>
          <div class="col-sm-9"><?php echo htmlspecialchars($course['created_at'] ?? ''); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Description</strong></div>
          <div class="col-sm-9"><?php echo nl2br(htmlspecialchars($course['description'] ?? '')); ?></div>
        </div>
      </div>
    </div>
  </div>

  <div class="col-md-4">
    <div class="card">
      <div class="card-header">
        <h5 class="mb-0">Stats</h5>
      </div>
      <div class="card-body">
        <div class="row text-center">
          <div class="col-6">
            <h4 class="mb-1"><?php echo number_format((int)($enrollments['cnt'] ?? 0)); ?></h4>
            <small class="text-muted">Enrollments</small>
          </div>
          <div class="col-6">
            <h4 class="mb-1"><?php echo round((float)($enrollments['avg_prog'] ?? 0), 1); ?>%</h4>
            <small class="text-muted">Avg Progress</small>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

<div class="row mt-4">
  <div class="col-12">
    <div class="card">
      <div class="card-header">
        <h5 class="mb-0">Modules & Lessons</h5>
      </div>
      <div class="card-body">
        <?php if (!$modules): ?>
          <p class="text-muted">No modules yet.</p>
        <?php else: foreach ($modules as $m): ?>
          <div class="border rounded p-3 mb-3">
            <h6 class="mb-1"><?php echo htmlspecialchars($m['title'] ?? ''); ?></h6>
            <small class="text-muted"><?php echo (int)($m['lesson_count'] ?? 0); ?> lessons</small>
          </div>
        <?php endforeach; endif; ?>
      </div>
    </div>
  </div>
</div>

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