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

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

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

// Region-scoped statistics
$db = Database::getInstance();
$rfCourses = currentRegionFilter('c');
$rfUsers = currentRegionFilter('u');

$stats = [
  'courses' => 0,
  'users' => 0,
  'enrollments' => 0,
  'revenue' => 0.0,
];

// Courses in region (published)
$row = $db->get(
  "SELECT COUNT(*) AS cnt FROM courses c WHERE {$rfCourses['clause']} AND c.status = 'published'",
  $rfCourses['params']
);
$stats['courses'] = (int)($row['cnt'] ?? 0);

// Active users in region
$row = $db->get(
  "SELECT COUNT(*) AS cnt FROM users u WHERE {$rfUsers['clause']} AND u.status = 'active'",
  $rfUsers['params']
);
$stats['users'] = (int)($row['cnt'] ?? 0);

// Enrollments for courses in region
$row = $db->get(
  "SELECT COUNT(*) AS cnt
   FROM course_enrollments ce
   JOIN courses c ON ce.course_id = c.id
   WHERE {$rfCourses['clause']}",
  $rfCourses['params']
);
$stats['enrollments'] = (int)($row['cnt'] ?? 0);

// Revenue from payments for courses in region (completed)
$row = $db->get(
  "SELECT COALESCE(SUM(p.amount), 0) AS total
   FROM payments p
   JOIN courses c ON p.course_id = c.id
   WHERE {$rfCourses['clause']} AND p.payment_status = 'completed'",
  $rfCourses['params']
);
$stats['revenue'] = (float)($row['total'] ?? 0);

$pageTitle = 'Regional Admin 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'] ?? 'Regional Admin'); ?></h5>
          <small class="text-body-secondary">Manage your region's courses, users and enrollments</small>
        </div>
        <span class="badge bg-label-primary">Region: <?php echo htmlspecialchars($_SESSION['region'] ?? DEFAULT_REGION); ?></span>
      </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-open-line icon-24px"></i></div>
              </div>
              <div class="ms-3">
                <p class="mb-0">Courses (region)</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-user-3-line icon-24px"></i></div>
              </div>
              <div class="ms-3">
                <p class="mb-0">Active Users</p>
                <h5 class="mb-0"><?php echo number_format($stats['users']); ?></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-task-line icon-24px"></i></div>
              </div>
              <div class="ms-3">
                <p class="mb-0">Enrollments</p>
                <h5 class="mb-0"><?php echo number_format($stats['enrollments']); ?></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-bar-chart-2-line icon-24px"></i></div>
              </div>
              <div class="ms-3">
                <p class="mb-0">Revenue</p>
                <h5 class="mb-0">$<?php echo number_format($stats['revenue'], 2); ?></h5>
              </div>
            </div>
          </div>
        </div>
        <div class="mt-4">
          <a href="../courses.php" class="btn btn-primary btn-sm me-2">Manage Courses</a>
          <a href="../dashboard.php" class="btn btn-outline-primary btn-sm">Back to Student View</a>
        </div>
      </div>
    </div>
  </div>
</div>

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