<?php
// admin/login_failures.php

session_start();
require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../auth/AuthController.php';

$auth = new AuthController();
if (!$auth->isLoggedIn()) {
    header('Location: ' . $adminLoginPath);
    exit;
}


$title = 'Fehlgeschlagene Logins';

ob_start();

try {
    $stmt = Database::getInstance()->query("SELECT id, email, failed_logins, last_login_attempt FROM admin_users WHERE failed_logins > 0 ORDER BY last_login_attempt DESC LIMIT 100");
    ErrorLogger::logSqlSuccess("Fehlgeschlagene Logins geladen | table: admin_users | context: " . json_encode(["filter" => "failed_logins > 0", "limit" => 100], JSON_UNESCAPED_UNICODE));
    $failures = $stmt->fetchAll();
} catch (PDOException $e) {
    ErrorLogger::logSqlError("Fehler beim Laden fehlgeschlagener Logins | table: admin_users | error: " . $e->getMessage());
    ErrorLogger::log("Fehler beim Laden der fehlgeschlagenen Logins: " . $e->getMessage());
    $failures = [];
}
?>

<div class="page-header">
  Nutzerprotokolle &raquo; Fehlgeschlagene Logins
</div>

<div class="page-contend">
  <table class="table table-bordered table-sm table-hover">
    <thead class="table-light">
      <tr>
        <th>ID</th>
        <th>E-Mail</th>
        <th>Fehlversuche</th>
        <th>Letzter Versuch</th>
      </tr>
    </thead>
    <tbody>
      <?php if (empty($failures)): ?>
        <tr><td colspan="4" class="text-center">Keine Fehlversuche gefunden.</td></tr>
      <?php else: ?>
        <?php foreach ($failures as $entry): ?>
          <tr>
            <td><?= $entry['id'] ?></td>
            <td><?= htmlspecialchars($entry['email']) ?></td>
            <td><?= $entry['failed_logins'] ?></td>
            <td><?= $entry['last_login_attempt'] ?></td>
          </tr>
        <?php endforeach; ?>
      <?php endif; ?>
    </tbody>
  </table>
</div>

<?php
$pageContent = ob_get_clean();
include __DIR__ . '/template.php';
