<?php
header('Content-Type: application/json');
date_default_timezone_set('Asia/Jakarta');
// --- Debug mentah input body HTTP
$raw = file_get_contents('php://input');
file_put_contents(__DIR__ . '/debug_raw_input.txt', date('c') . ' - RAW: ' . var_export($raw, true) . PHP_EOL, FILE_APPEND);

// --- Jika kosong, kirim error
if (trim($raw) === '') {
    http_response_code(400);
    echo json_encode(['status' => 'invalid JSON', 'raw' => 'EMPTY']);
    exit;
}

// --- Decode JSON
$input = json_decode($raw, true);

// --- Cek error JSON
if (json_last_error() !== JSON_ERROR_NONE) {
    http_response_code(400);
    echo json_encode(['status' => 'invalid JSON', 'raw' => $raw]);
    exit;
}

// --- Simpan ke file log
$log_file = __DIR__ . '/tracking_log.txt';
file_put_contents($log_file, date('c') . ' - ' . json_encode($input) . PHP_EOL, FILE_APPEND);

// --- Balas sukses
echo json_encode(['status' => 'ok']);
