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

$file = __DIR__ . '/data.json';

$raw = file_get_contents('php://input');
if(!$raw){ http_response_code(400); echo json_encode(['error'=>'no data']); exit; }

$data = json_decode($raw);
if(json_last_error() !== JSON_ERROR_NONE){ http_response_code(400); echo json_encode(['error'=>'invalid json']); exit; }

file_put_contents($file, json_encode($data, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));
echo json_encode(['ok'=>true]);
