<?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();
$logs=safeFetchAll($pdo,"SELECT l.*,
 CASE l.user_type WHEN 'student' THEN s.full_name WHEN 'teacher' THEN COALESCE(t.full_name,t.name) WHEN 'admin' THEN COALESCE(a.full_name,a.name) ELSE 'System' END AS user_name
 FROM core_activity_logs l
 LEFT JOIN students s ON l.user_type='student' AND s.id=l.user_id
 LEFT JOIN core_teachers t ON l.user_type='teacher' AND t.id=l.user_id
 LEFT JOIN core_admins a ON l.user_type='admin' AND a.id=l.user_id
 ORDER BY l.id DESC LIMIT 500");
?>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Activity Logs</title>
<style>body{font-family:Inter,Arial,sans-serif;background:#f1f5f9;padding:30px;color:#0f172a}.wrap{max-width:1400px;margin:auto}.top{display:flex;justify-content:space-between;align-items:center;margin-bottom:24px}.back{padding:12px 18px;background:#e0e7ff;border-radius:14px;text-decoration:none;color:#3730a3;font-weight:800}.card{background:#fff;padding:28px;border-radius:28px;box-shadow:0 20px 50px rgba(15,23,42,.08);overflow:auto}table{width:100%;border-collapse:collapse;min-width:900px}th,td{padding:15px;border-top:1px solid #e2e8f0;text-align:left;vertical-align:top}th{background:#f8fafc}.badge{padding:6px 10px;border-radius:999px;background:#dbeafe;color:#1d4ed8;font-size:12px;font-weight:800}.muted{color:#64748b;font-size:13px}</style></head><body><div class="wrap"><div class="top"><div><h1>Activity Logs</h1><p class="muted">Latest 500 platform actions</p></div><a class="back" href="dashboard.php">Dashboard</a></div><div class="card"><table><thead><tr><th>User</th><th>Role</th><th>Action</th><th>Description</th><th>IP</th><th>Date</th></tr></thead><tbody>
<?php foreach($logs as $log): ?><tr><td><?=e($log['user_name'] ?? 'System')?></td><td><span class="badge"><?=e($log['user_type'] ?? 'system')?></span></td><td><?=e($log['action'] ?? '-')?></td><td><?=e($log['description'] ?? '-')?></td><td><?=e($log['ip_address'] ?? '-')?></td><td><?=e(formatDateTimeNice($log['created_at'] ?? null))?></td></tr><?php endforeach; ?>
<?php if(!$logs): ?><tr><td colspan="6">No logs found.</td></tr><?php endif; ?></tbody></table></div></div></body></html>
