<?php
require_once(dirname(__DIR__) . '/init.php');

// Güvenli session başlat
m_secure_session_start();

// Admin kontrolü
if (!m_admin_check()) {
    echo "Admin girişi gerekli!";
    exit;
}

// Session güvenlik kontrolü
m_validate_session_security();

echo "<h1>Security Monitoring ve Raporlama Test (MADDE 3)</h1>";

// Test fonksiyonları
if (isset($_GET['test'])) {
    $test_type = $_GET['test'];
    
    switch ($test_type) {
        case 'dashboard':
            echo "<h2>Security Dashboard Test</h2>";
            $dashboard = m_get_security_dashboard();
            echo "<pre>";
            print_r($dashboard);
            echo "</pre>";
            break;
            
        case 'security_scan':
            echo "<h2>Security Scan Test</h2>";
            $scan_results = m_perform_security_scan();
            echo "<pre>";
            print_r($scan_results);
            echo "</pre>";
            break;
            
        case 'create_alert':
            echo "<h2>Security Alert Test</h2>";
            $result = m_create_security_alert('test_alert', 'medium', [
                'test' => 'data',
                'timestamp' => time()
            ]);
            echo "Alert Created: " . ($result ? 'Success' : 'Failed') . "<br>";
            break;
            
        case 'generate_report':
            echo "<h2>Security Report Test</h2>";
            $report = m_generate_security_report('daily');
            echo "<pre>";
            print_r($report);
            echo "</pre>";
            break;
            
        case 'update_stats':
            echo "<h2>Update Security Stats Test</h2>";
            m_update_security_stats('test_event');
            echo "Stats Updated<br>";
            break;
            
        case 'mark_alert_read':
            echo "<h2>Mark Alert as Read Test</h2>";
            // Önce bir alert oluştur
            m_create_security_alert('test_read_alert', 'low');
            
            // Son alert'i bul ve okundu olarak işaretle
            try {
                $db = new DB();
                $last_alert = $db->table('security_alerts')
                    ->where('1', '=', '1')
                    ->get();
                
                // Manuel sıralama ve limit
                if (!empty($last_alert['data'])) {
                    usort($last_alert['data'], function($a, $b) {
                        return $b['id'] - $a['id'];
                    });
                    $last_alert['data'] = array_slice($last_alert['data'], 0, 1);
                }
                
                if (!empty($last_alert['data'])) {
                    $result = m_mark_alert_as_read($last_alert['data'][0]['id']);
                    echo "Alert Marked as Read: " . ($result ? 'Success' : 'Failed') . "<br>";
                }
            } catch (Exception $e) {
                echo "Error: " . $e->getMessage() . "<br>";
            }
            break;
    }
}

echo "<h2>Test Linkleri:</h2>";
echo "<a href='?test=dashboard'>Security Dashboard</a><br>";
echo "<a href='?test=security_scan'>Security Scan</a><br>";
echo "<a href='?test=create_alert'>Create Alert</a><br>";
echo "<a href='?test=generate_report'>Generate Report</a><br>";
echo "<a href='?test=update_stats'>Update Stats</a><br>";
echo "<a href='?test=mark_alert_read'>Mark Alert as Read</a><br>";

echo "<h2>Mevcut Durum:</h2>";
echo "<p>IP Adresi: " . m_get_client_ip() . "</p>";
echo "<p>Session ID: " . session_id() . "</p>";
echo "<p>Admin ID: " . ($_SESSION['m_admin'] ?? 'Yok') . "</p>";

echo "<h2>Security Monitoring Özellikleri:</h2>";
echo "<ul>";
echo "<li>Güvenlik Dashboard - Gerçek zamanlı metrikler</li>";
echo "<li>Security Scan - Sistem güvenlik taraması</li>";
echo "<li>Security Alerts - Güvenlik alarmları</li>";
echo "<li>Security Reports - Günlük/haftalık raporlar</li>";
echo "<li>Security Stats - İstatistik takibi</li>";
echo "<li>Alert Management - Alarm yönetimi</li>";
echo "</ul>";

echo "<h2>Dashboard Metrikleri:</h2>";
$dashboard = m_get_security_dashboard();
if (!empty($dashboard)) {
    echo "<ul>";
    foreach ($dashboard as $key => $value) {
        if (!is_array($value)) {
            echo "<li><strong>$key:</strong> $value</li>";
        }
    }
    echo "</ul>";
}

echo "<h2>Son Güvenlik Olayları:</h2>";
if (!empty($dashboard['recent_events'])) {
    echo "<ul>";
    foreach ($dashboard['recent_events'] as $event) {
        echo "<li><strong>" . $event['event_type'] . "</strong> - " . $event['created_at'] . " (IP: " . $event['ip_address'] . ")</li>";
    }
    echo "</ul>";
} else {
    echo "<p>Henüz güvenlik olayı yok.</p>";
}

echo "<h2>Okunmamış Alarmlar:</h2>";
if (!empty($dashboard['unread_alerts'])) {
    echo "<ul>";
    foreach ($dashboard['unread_alerts'] as $alert) {
        echo "<li><strong>" . $alert['alert_type'] . "</strong> (" . $alert['severity'] . ") - " . $alert['message'] . "</li>";
    }
    echo "</ul>";
} else {
    echo "<p>Okunmamış alarm yok.</p>";
}
?> 