<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
iscms_require_roles(['SUPER_ADMIN', 'ADMIN']);

$pageTitle = 'Audit logs';
$activeNav = 'audit_logs';

$sql = "SELECT a.id, a.action, a.module, a.entity_type, a.entity_id, a.description,
               a.ip_address, a.request_method, a.request_path, a.created_at,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS user_name,
               u.email AS user_email
        FROM audit_logs a
        LEFT JOIN users u ON a.user_id = u.id
        ORDER BY a.created_at DESC
        LIMIT 500";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>Audit logs</h1>
        <p>System audit trail: actions, modules, and request metadata.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Related</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="activity_log.php"><i class="ri-pulse-line"></i> Activity log</a>
            <a class="qa-btn" href="list_users.php"><i class="ri-team-line"></i> Users</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-file-code-line"></i> Entries</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>When</th>
                        <th>User</th>
                        <th>Action</th>
                        <th>Module</th>
                        <th>Entity</th>
                        <th>IP</th>
                        <th>Method</th>
                        <th>Path</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="8"><i class="ri-inbox-line me-2"></i>No audit entries</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $desc = (string) ($r['description'] ?? '');
                            $ent = trim((string) ($r['entity_type'] ?? '') . ' ' . (string) ($r['entity_id'] ?? ''));
                            $ent = trim($ent);
                        ?>
                        <tr>
                            <td><small class="text-muted"><?= !empty($r['created_at']) ? iscms_h(substr((string) $r['created_at'], 0, 19)) : '-' ?></small></td>
                            <td><small><?php
                                $nm = trim((string) ($r['user_name'] ?? ''));
                                echo $nm !== '' ? iscms_h($nm) : '<span class="text-muted">System</span>';
                            ?></small></td>
                            <td><small><?= iscms_h((string) ($r['action'] ?? '')) ?></small></td>
                            <td><small><?= iscms_h((string) ($r['module'] ?? '')) ?></small></td>
                            <td><small><?= $ent !== '' ? iscms_h($ent) : '<span class="text-muted">-</span>' ?></small></td>
                            <td class="iscms-mono"><small><?= iscms_val($r['ip_address'] ?? null) ?></small></td>
                            <td><small><?= iscms_val($r['request_method'] ?? null) ?></small></td>
                            <td><small title="<?= iscms_h((string) ($r['request_path'] ?? '')) ?>"><?php
                                $p = (string) ($r['request_path'] ?? '');
                                echo iscms_h(strlen($p) > 40 ? substr($p, 0, 37) . '...' : $p);
                            ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
