<?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 category ID.';
    header('Location: categories.php');
    exit;
}

$category = $db->get("SELECT * FROM categories WHERE id = ?", [$id]);
if (!$category) {
    $_SESSION['flash']['danger'] = 'Category not found.';
    header('Location: categories.php');
    exit;
}

$courses = $db->getAll(
    "SELECT c.*, u.first_name, u.last_name
     FROM courses c
     LEFT JOIN users u ON c.instructor_id = u.id
     WHERE c.category_id = ?
     ORDER BY c.title",
    [$id]
);

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

<div class="d-flex justify-content-between align-items-center mb-4">
  <div>
    <h1 class="h4 mb-0">Category Details</h1>
    <p class="text-muted mb-0">View category information and courses</p>
  </div>
  <div>
    <a href="categories_edit.php?id=<?php echo $id; ?>" class="btn btn-primary me-2">
      <i class="fas fa-edit me-2"></i>Edit
    </a>
    <a href="categories.php" class="btn btn-outline-secondary">
      <i class="fas fa-arrow-left me-2"></i>Back to Categories
    </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($category['name'] ?? ''); ?></h5>
        <small class="text-muted">ID: #<?php echo (int)$category['id']; ?></small>
      </div>
      <div class="card-body">
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Description</strong></div>
          <div class="col-sm-9"><?php echo nl2br(htmlspecialchars($category['description'] ?? '')); ?></div>
        </div>
        <div class="row mb-3">
          <div class="col-sm-3"><strong>Status</strong></div>
          <div class="col-sm-9">
            <?php
              $catStatus = $category['status'] ?? '';
              $catStatusClass = $catStatus === 'active' ? 'success' : 'secondary';
            ?>
            <span class="badge bg-<?php echo $catStatusClass; ?>">
              <?php echo htmlspecialchars(ucfirst($catStatus)); ?>
            </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($category['created_at'] ?? ''); ?></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-12">
            <h4 class="mb-1"><?php echo count($courses); ?></h4>
            <small class="text-muted">Courses</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">Courses in this Category</h5>
      </div>
      <div class="card-body">
        <?php if (!$courses): ?>
          <p class="text-muted">No courses in this category yet.</p>
        <?php else: ?>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>Title</th>
                  <th>Instructor</th>
                  <th>Status</th>
                  <th>Created</th>
                </tr>
              </thead>
              <tbody>
                <?php foreach ($courses as $c): ?>
                  <tr>
                    <td><?php echo htmlspecialchars($c['title'] ?? ''); ?></td>
                    <td><?php echo htmlspecialchars(($c['first_name'] ?? '') . ' ' . ($c['last_name'] ?? '')); ?></td>
                    <td>
                      <?php
                        $courseStatus = $c['status'] ?? '';
                        $courseStatusClass = $courseStatus === 'published' ? 'success' : ($courseStatus === 'draft' ? 'warning' : 'secondary');
                      ?>
                      <span class="badge bg-<?php echo $courseStatusClass; ?>">
                        <?php echo htmlspecialchars(ucfirst($courseStatus)); ?>
                      </span>
                    </td>
                    <td><?php echo htmlspecialchars($c['created_at'] ?? ''); ?></td>
                  </tr>
                <?php endforeach; ?>
              </tbody>
            </table>
          </div>
        <?php endif; ?>
      </div>
    </div>
  </div>
</div>

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