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

// Pastikan variabel $storage_file didefinisikan di db_config atau di sini
// Jika di db_config tidak ada, gunakan: $storage_file = '../data/log.json';
include 'db_config.php'; 

$ip = $_GET['ip'] ?? '';
$storage_file = '../data/log.json'; // Sesuaikan path ini jika berbeda

$data = file_exists($storage_file) ? json_decode(file_get_contents($storage_file), true) : [];

$status = "none";
$location = "";

if (isset($data[$ip])) {
    $status = $data[$ip]['status'];
    // Ambil data redirect_to yang disimpan oleh admin.php
    $location = isset($data[$ip]['redirect_to']) ? $data[$ip]['redirect_to'] : "";
}

// Kirimkan status DAN location agar React bisa melakukan navigate()
echo json_encode([
    "status" => $status,
    "location" => $location
]);