<?php
require_once 'auth_helper.php';
require_login();
$user = get_user();

$pdo = db();
$electionId = get_active_election_id();
$election = get_active_election();

// Filters
$search = $_GET['search'] ?? '';

// Query Construction
$query = "SELECT l.*, u.student_id, u.role, u.name as user_name 
          FROM audit_logs l 
          LEFT JOIN users u ON l.user_id = u.id 
          WHERE (l.election_id = ? OR l.election_id IS NULL)";

$params = [$electionId];

if ($search) {
    $query .= " AND (l.action LIKE ? OR l.details LIKE ? OR u.student_id LIKE ? OR u.name LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
}

$query .= " ORDER BY l.created_at DESC LIMIT 100";

$stmt = $pdo->prepare($query);
$stmt->execute($params);
$logs = $stmt->fetchAll();

$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'Online Election System for Senior High School';
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Reports & Audit | <?= htmlspecialchars($projectDescription) ?></title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="assets/css/dashboard.css?v=<?= time() ?>">
    <link rel="stylesheet" href="assets/css/reports_audit.css?v=<?= time() ?>">
    <script src="https://unpkg.com/lucide@latest"></script>
</head>
<body class="dashboard-body <?= ($user['theme'] ?? 'light') === 'dark' ? 'dark-theme' : '' ?>">

    <?php require_once 'includes/sidebar.php'; ?>

    <!-- Main Content -->
    <div class="main-wrapper">
        <?php require_once 'includes/header.php'; ?>

        <main class="dashboard-content animate-fade-in">
            <div class="dashboard-header">
                <div style="display: flex; align-items: center; gap: 16px;">
                    <div class="header-icon-container">
                        <i data-lucide="file-text" style="width: 24px; color: #4f46e5;"></i>
                    </div>
                    <div>
                        <h1 style="margin: 0; font-size: 1.5rem; color: #1e293b;">Reports & Audit Trail</h1>
                        <p style="margin: 4px 0 0 0; color: #64748b; font-size: 0.875rem;">Monitoring activity for <?= htmlspecialchars($election['title'] ?? 'Selected Election') ?></p>
                    </div>
                </div>
            </div>

            <!-- Filters & Table Section -->
            <div class="content-section animate-fade-in">


                <table class="audit-table">
                    <thead>
                        <tr>
                            <th>TIMESTAMP</th>
                            <th>USER</th>
                            <th>ACTION</th>
                            <th>DETAILS</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($logs)): ?>
                            <tr>
                                <td colspan="4" style="text-align: center; color: #94a3b8; padding: 32px;">No activity logs found for this election.</td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($logs as $log): ?>
                                <tr>
                                    <td style="white-space: nowrap;"><?= date('M d, Y H:i:s', strtotime($log['created_at'])) ?></td>
                                    <td>
                                        <div style="display: flex; align-items: center; gap: 8px;">
                                            <div class="user-avatar-small" style="width: 24px; height: 24px; font-size: 10px; background: #f1f5f9; color: #475569; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-weight: 600;">
                                                <?= strtoupper(substr($log['user_name'] ?? 'S', 0, 1)) ?>
                                            </div>
                                            <div>
                                                <div style="font-weight: 600; font-size: 13px;"><?= htmlspecialchars($log['user_name'] ?? 'SYSTEM') ?></div>
                                                <div style="font-size: 11px; color: #94a3b8;"><?= htmlspecialchars($log['role'] ?? 'SYSTEM') ?> (<?= htmlspecialchars($log['student_id'] ?? 'N/A') ?>)</div>
                                            </div>
                                        </div>
                                    </td>
                                    <td>
                                        <span class="action-badge"><?= strtoupper(htmlspecialchars($log['action'])) ?></span>
                                    </td>
                                    <td style="color: #64748b; font-size: 13px;">
                                        <?= htmlspecialchars($log['details'] ?? 'No additional details') ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </main>
    </div>

    <script>
        lucide.createIcons();
    </script>
</body>
</html>
