<?php
/**
 * IBVTE Admin - System Audit Logs
 */
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/auth.php';

if (!checkDBConnection()) exit;
Auth()->requireRole(['super_admin', 'admin']);

$db = DB();

$page = max(1, intval($_GET['page'] ?? 1));
$limit = 20;
$offset = ($page - 1) * $limit;

$logs = $db->fetchAll("
    SELECT al.*, u.full_name, u.username, r.role_slug, r.role_name
    FROM audit_logs al 
    LEFT JOIN users u ON al.user_id = u.id 
    LEFT JOIN roles r ON u.role_id = r.id
    ORDER BY al.created_at DESC 
    LIMIT $limit OFFSET $offset
");

$totalLogs = $db->fetchColumn("SELECT COUNT(*) FROM audit_logs");
$totalPages = ceil($totalLogs / $limit);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>System Audit Logs - <?= APP_SHORT_NAME ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="../assets/css/admin-style.css" rel="stylesheet">
</head>
<body>
    <?php include __DIR__ . '/../includes/admin_header.php'; ?>
    <div class="container-fluid">
        <div class="row">
            <?php include __DIR__ . '/../includes/admin_sidebar.php'; ?>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 py-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2 class="h3 fw-bold"><i class="fas fa-list-alt me-2 text-primary"></i>System Audit Logs</h2>
                </div>

                <div class="card shadow-sm border-0 rounded-4">
                    <div class="card-body p-0">
                        <div class="table-responsive">
                            <table class="table table-hover align-middle mb-0">
                                <thead class="bg-light">
                                    <tr>
                                        <th class="ps-4">User</th>
                                        <th>Action</th>
                                        <th>Entity</th>
                                        <th>IP Address</th>
                                        <th>Timestamp</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php if (empty($logs)): ?>
                                        <tr><td colspan="5" class="text-center py-5 text-muted">No audit logs found.</td></tr>
                                    <?php endif; ?>
                                    <?php foreach ($logs as $log): ?>
                                    <tr>
                                        <td class="ps-4">
                                            <div class="fw-bold"><?= htmlspecialchars($log['full_name'] ?? 'System') ?></div>
                                            <div class="small text-muted"><?= htmlspecialchars($log['username'] ?? '') ?> (<?= strtoupper($log['role_slug'] ?? '') ?>)</div>
                                        </td>
                                        <td>
                                            <span class="badge bg-<?= strpos(strtolower($log['action']), 'delete') !== false ? 'danger' : (strpos(strtolower($log['action']), 'update') !== false ? 'info' : 'primary') ?>">
                                                <?= htmlspecialchars($log['action']) ?>
                                            </span>
                                        </td>
                                        <td>
                                            <div class="small fw-bold text-dark"><?= ucfirst($log['entity_type'] ?? 'N/A') ?></div>
                                            <div class="small text-muted">ID: <?= $log['entity_id'] ?? 'N/A' ?></div>
                                        </td>
                                        <td><span class="small text-muted"><?= htmlspecialchars($log['ip_address'] ?? 'N/A') ?></span></td>
                                        <td><div class="small fw-bold"><?= date('d M Y, h:i A', strtotime($log['created_at'])) ?></div></td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <?php if ($totalPages > 1): ?>
                <nav class="mt-4">
                    <ul class="pagination justify-content-center">
                        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                        <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                            <a class="page-link" href="?page=<?= $i ?>"><?= $i ?></a>
                        </li>
                        <?php endfor; ?>
                    </ul>
                </nav>
                <?php endif; ?>
            </main>
        </div>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>