<?php
header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['ip']) && isset($_POST['last_seen'])) {
    $ip = $_POST['ip'];
    $last_seen = intval($_POST['last_seen']);
    
    $logs_file = __DIR__ . '/files/logs/all_logs.json';
    
    if (file_exists($logs_file)) {
        $json_data = json_decode(file_get_contents($logs_file), true);
        if (json_last_error() === JSON_ERROR_NONE) {
            $updated = false;
            
            foreach ($json_data['all_logs'] as &$entry) {
                if ($entry['ip'] === $ip) {
                    $entry['last_seen'] = $last_seen;
                    $updated = true;
                    break;
                }
            }
            
            if ($updated) {
                file_put_contents($logs_file, json_encode($json_data, JSON_PRETTY_PRINT));
                echo json_encode(['success' => true]);
                exit;
            }
        }
    }
    
    echo json_encode(['success' => false, 'error' => 'Failed to update status']);
    exit;
}

echo json_encode(['success' => false, 'error' => 'Invalid request']);
exit; 