<?php
require_once 'config.php';
requireAdmin();

$activeTab = $_GET['tab'] ?? 'attacks';
$pdo = getDBConnection();

// Get logs based on tab
$attacks = $pdo->query("SELECT * FROM attack_logs ORDER BY timestamp DESC LIMIT 100")->fetchAll();
$defenses = $pdo->query("SELECT * FROM defense_logs ORDER BY timestamp DESC LIMIT 100")->fetchAll();
$malware = $pdo->query("SELECT * FROM malware_logs ORDER BY timestamp DESC LIMIT 100")->fetchAll();
$aiInjections = $pdo->query("SELECT * FROM ai_injection_logs ORDER BY timestamp DESC LIMIT 100")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>System Logs - <?php echo APP_NAME; ?></title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <nav class="navbar">
            <div class="logo">
                <h1>📜 System Logs</h1>
                <small>Audit Trail & Activity Monitoring</small>
            </div>
            <div class="nav-links">
                <a href="index.php">Dashboard</a>
                <a href="attack.php">Attack Mode</a>
                <a href="defense.php">Defense Mode</a>
                <a href="malware_analysis.php">Malware Analysis</a>
                <a href="reports.php">Reports</a>
                <a href="logs.php" class="active">Logs</a>
                <a href="admin_setup.php">Admin Setup</a>
                <a href="index.php?logout=1" class="logout">Logout</a>
            </div>
        </nav>
        
        <div class="content">
            <div class="log-tabs">
                <a href="?tab=attacks" class="tab <?php echo $activeTab == 'attacks' ? 'active' : ''; ?>">⚔️ Attack Logs (<?php echo count($attacks); ?>)</a>
                <a href="?tab=defenses" class="tab <?php echo $activeTab == 'defenses' ? 'active' : ''; ?>">🛡️ Defense Logs (<?php echo count($defenses); ?>)</a>
                <a href="?tab=malware" class="tab <?php echo $activeTab == 'malware' ? 'active' : ''; ?>">🐛 Malware Analysis (<?php echo count($malware); ?>)</a>
                <a href="?tab=ai" class="tab <?php echo $activeTab == 'ai' ? 'active' : ''; ?>">🤖 AI Injection (<?php echo count($aiInjections); ?>)</a>
            </div>
            
            <?php if($activeTab == 'attacks'): ?>
            <div class="card">
                <h3>⚔️ Attack Simulation Logs</h3>
                <table class="data-table">
                    <thead>
                        <tr><th>Timestamp</th><th>Attack Type</th><th>Target</th><th>Severity</th><th>Payload</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach($attacks as $log): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($log['timestamp']); ?></td>
                            <td><?php echo htmlspecialchars($log['attack_type']); ?></td>
                            <td><?php echo htmlspecialchars(substr($log['target_url'], 0, 50)); ?></td>
                            <td class="severity-<?php echo strtolower($log['severity']); ?>"><?php echo htmlspecialchars($log['severity']); ?></td>
                            <td><code><?php echo htmlspecialchars(substr($log['payload'], 0, 60)); ?></code></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
            
            <?php if($activeTab == 'defenses'): ?>
            <div class="card">
                <h3>🛡️ Defense Scan Logs</h3>
                <table class="data-table">
                    <thead>
                        <tr><th>Timestamp</th><th>Scan Type</th><th>Target</th><th>Risk Score</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach($defenses as $log): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($log['timestamp']); ?></td>
                            <td><?php echo htmlspecialchars($log['defense_type']); ?></td>
                            <td><?php echo htmlspecialchars(substr($log['target_url'], 0, 50)); ?></td>
                            <td><?php echo $log['risk_score']; ?>/100</td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
            
            <?php if($activeTab == 'malware'): ?>
            <div class="card">
                <h3>🐛 Malware Analysis Logs</h3>
                <table class="data-table">
                    <thead>
                        <tr><th>Timestamp</th><th>File/Hash</th><th>Detection Rate</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach($malware as $log): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($log['timestamp']); ?></td>
                            <td><code><?php echo htmlspecialchars(substr($log['file_hash'], 0, 16)) . '...'; ?></code></td>
                            <td><?php echo $log['malicious_count']; ?>/<?php echo $log['total_engines']; ?> engines</td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
            
            <?php if($activeTab == 'ai'): ?>
            <div class="card">
                <h3>🤖 AI Injection Logs</h3>
                <table class="data-table">
                    <thead>
                        <tr><th>Timestamp</th><th>Target AI</th><th>Prompt Preview</th><th>Success</th></tr>
                    </thead>
                    <tbody>
                        <?php foreach($aiInjections as $log): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($log['timestamp']); ?></td>
                            <td><?php echo htmlspecialchars($log['target_ai_endpoint']); ?></td>
                            <td><?php echo htmlspecialchars(substr($log['injection_prompt'], 0, 60)); ?>...</td>
                            <td><?php echo $log['success'] ? '⚠️ Bypassed' : '✅ Blocked'; ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>