<?php
header('Content-Type: application/json');

$dir = __DIR__;
$device = trim($_POST['device_id'] ?? '');
$status = trim($_POST['status'] ?? 'sent'); // "sent" | "failed" | "delivered"
$msg    = (string)($_POST['message'] ?? '');
$to     = trim($_POST['to'] ?? '');

if ($device===''){ echo json_encode(['ok'=>false]); exit; }

$ackFile = $dir . '/ack_' . preg_replace('/[^a-zA-Z0-9_\-]/','_', $device) . '.json';
file_put_contents($ackFile, json_encode([
  'status'=>$status,'message'=>$msg,'to'=>$to,'ts'=>time()
], JSON_UNESCAPED_UNICODE));

echo json_encode(['ok'=>true]);
