<?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();

$rows = [];

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

    $rows = $pdo->query("
        SELECT
            a.*,
            s.full_name,
            s.student_code
        FROM core_attendance a
        LEFT JOIN students s
            ON s.id = a.student_id
        ORDER BY a.id DESC
    ")->fetchAll();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Attendance Reports</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}
.present{color:#166534;font-weight:900}
.late{color:#b45309;font-weight:900}
.absent{color:#b91c1c;font-weight:900}
</style>
</head>
<body>

<div class="card">

<h1>Attendance Reports</h1>

<table>

<thead>
<tr>
<th>Student</th>
<th>Code</th>
<th>Status</th>
<th>Minutes</th>
<th>Date</th>
</tr>
</thead>

<tbody>

<?php if($rows): ?>

<?php foreach($rows as $row): ?>

<tr>

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

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

<td class="<?php echo e($row['status']); ?>">
<?php echo e(ucfirst($row['status'])); ?>
</td>

<td><?php echo e($row['minutes_attended'] ?? '0'); ?></td>

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

</tr>

<?php endforeach; ?>

<?php else: ?>

<tr>
<td colspan="5">No attendance reports found.</td>
</tr>

<?php endif; ?>

</tbody>

</table>

</div>

</body>
</html>