<?php
/**
 * Admin - Failed login attempts
 * PHP User Registration - https://rskworld.in
 * Developer: Molla Samser | RSK World
 * Contact: help@rskworld.in | +91 93305 39277
 * Year: 2026
 */

require_once __DIR__ . '/../includes/init.php';
require_once __DIR__ . '/../includes/functions.php';

requireAdmin();
$baseHref = '..';

$pdo = getDbConnection();
$page = max(1, (int) ($_GET['page'] ?? 1));
$perPage = 50;
$total = 0;
$attempts = [];

try {
    $total = (int) $pdo->query('SELECT COUNT(*) FROM failed_login_attempts')->fetchColumn();
} catch (PDOException $e) {
    if ($e->getCode() === '42S02') {
        $pdo->exec("CREATE TABLE IF NOT EXISTS failed_login_attempts (
            id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            email VARCHAR(255) NOT NULL,
            ip_address VARCHAR(45) NULL,
            attempted_at DATETIME DEFAULT CURRENT_TIMESTAMP,
            INDEX idx_email (email),
            INDEX idx_attempted (attempted_at)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
    }
}
$totalPages = max(1, (int) ceil($total / $perPage));
$page = min($page, $totalPages);
$offset = ($page - 1) * $perPage;
try {
    $stmt = $pdo->prepare('SELECT id, email, ip_address, attempted_at FROM failed_login_attempts ORDER BY attempted_at DESC LIMIT ? OFFSET ?');
    $stmt->execute([$perPage, $offset]);
    $attempts = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    // table just created, empty
}

$pageTitle = 'Admin - Failed Logins';
require_once __DIR__ . '/../includes/header.php';
?>

<div class="page-content admin-content">
    <h1><i class="fas fa-exclamation-triangle"></i> Failed Login Attempts</h1>
    <p class="subtitle">PHP User Registration — <a href="https://rskworld.in" target="_blank">rskworld.in</a></p>

    <div class="table-wrap">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Email</th>
                    <th>IP</th>
                    <th>Attempted at</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($attempts as $a): ?>
                <tr>
                    <td><?= (int) ($a['id'] ?? 0) ?></td>
                    <td><?= htmlspecialchars($a['email'] ?? '') ?></td>
                    <td><?= htmlspecialchars($a['ip_address'] ?? '—') ?></td>
                    <td><?= !empty($a['attempted_at']) ? date('M j, Y g:i A', strtotime($a['attempted_at'])) : '—' ?></td>
                </tr>
                <?php endforeach; ?>
                <?php if (empty($attempts)): ?>
                <tr><td colspan="4" class="text-muted">No failed login attempts recorded.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if ($totalPages > 1): ?>
    <div class="pagination">
        <?php if ($page > 1): ?>
            <a href="failed-logins.php?page=<?= $page - 1 ?>">« Previous</a>
        <?php endif; ?>
        <span class="pagination-info">Page <?= $page ?> of <?= $totalPages ?> (<?= $total ?> attempts)</span>
        <?php if ($page < $totalPages): ?>
            <a href="failed-logins.php?page=<?= $page + 1 ?>">Next »</a>
        <?php endif; ?>
    </div>
    <?php endif; ?>

    <p><a href="index.php" class="btn btn-secondary">Back to Admin</a></p>
</div>

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