<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/admin-auth.php';

$pdo = db();
requireAdmin();

$teachers = [];

if (tableExists($pdo, 'core_teachers')) {

    $teachers = $pdo->query("
        SELECT
            t.*,

            (
                SELECT COUNT(*)
                FROM core_courses c
                WHERE c.teacher_id = t.id
            ) AS total_courses

        FROM core_teachers t
        ORDER BY total_courses DESC
    ")->fetchAll();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Teacher Performance</title>

<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800;900&display=swap" rel="stylesheet">

<style>
body{font-family:Inter,sans-serif;background:#f1f5f9;padding:30px}
.card{background:#fff;padding:30px;border-radius:30px}
table{width:100%;border-collapse:collapse}
th,td{padding:16px;border-top:1px solid #e2e8f0;text-align:left}
th{background:#f8fafc}
.badge{padding:6px 10px;border-radius:999px;background:#dbeafe;color:#1d4ed8;font-size:12px;font-weight:800}
</style>
</head>
<body>

<div class="card">

<h1>Teacher Performance</h1>

<table>

<thead>
<tr>
<th>Teacher</th>
<th>Email</th>
<th>Courses</th>
<th>Status</th>
</tr>
</thead>

<tbody>

<?php if($teachers): ?>

<?php foreach($teachers as $teacher): ?>

<tr>

<td>
<?php echo e($teacher['full_name'] ?? $teacher['name'] ?? 'N/A'); ?>
</td>

<td>
<?php echo e($teacher['email'] ?? '-'); ?>
</td>

<td>
<span class="badge">
<?php echo e((string)$teacher['total_courses']); ?>
Courses
</span>
</td>

<td>
<?php echo e(ucfirst($teacher['status'] ?? 'active')); ?>
</td>

</tr>

<?php endforeach; ?>

<?php else: ?>

<tr>
<td colspan="4">No teachers found.</td>
</tr>

<?php endif; ?>

</tbody>

</table>

</div>

</body>
</html>