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

$dir = __DIR__;
$device = trim($_POST['device_id'] ?? '');
if ($device===''){ echo json_encode(['ok'=>false]); exit; }

$ackFile = $dir . '/ack_' . preg_replace('/[^a-zA-Z0-9_\-]/','_', $device) . '.json';
if (!is_file($ackFile)){ echo json_encode(['ok'=>true,'data'=>null]); exit; }

$raw = file_get_contents($ackFile);
$data = json_decode($raw, true);

// optional: auto-expire after 60s
if (!is_array($data) || (time() - (int)($data['ts'] ?? 0)) > 60){
  @unlink($ackFile);
  echo json_encode(['ok'=>true,'data'=>null]); exit;
}

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