<?php
header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");

$uid = $_GET['uid'] ?? '';
if (!$uid) {
    echo json_encode(['status' => 'error', 'message' => 'UID diperlukan']);
    exit;
}

$file = "user/uid" . $uid . ".txt";

if (!file_exists($file)) {
    echo json_encode(['status' => 'empty', 'data' => []]);
    exit;
}

// Baca file baris per baris
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$logData = [];

// Balik urutan agar yang terbaru di atas
$lines = array_reverse($lines);

foreach ($lines as $line) {
    $decoded = json_decode($line, true);
    if ($decoded) {
        $logData[] = $decoded;
    }
}

echo json_encode(['status' => 'success', 'data' => $logData]);
?>