<?php
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Headers: Content-Type");

$file = __DIR__ . "/database.json";

// ambil body JSON dari fetch
$raw = file_get_contents("php://input");
$data = json_decode($raw, true);

if (!$data || !is_array($data)) {
    http_response_code(400);
    echo json_encode([
        "ok" => false,
        "error" => "Data tidak valid"
    ]);
    exit;
}

// overwrite (hanya 1 data)
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));

echo json_encode([
    "ok" => true,
    "message" => "Data berhasil disimpan"
]);