<?php
header("Content-Type: application/json");
$json_file = 'database.json';

// 1. Lire la DB
if (!file_exists($json_file)) die(json_encode(["status"=>"error"]));
$db = json_decode(file_get_contents($json_file), true);

// 2. Récupérer les infos
$client_in = $_REQUEST['client'] ?? '';
$token_in  = $_REQUEST['token'] ?? '';
$user      = $_REQUEST['user'] ?? 'Inconnu';

// 3. Trouver le client
$idx = -1;
foreach ($db['clients'] as $i => $c) {
    if (strcasecmp($c['name'], $client_in)==0 && $c['token']===$token_in) {
        $idx = $i; break;
    }
}
if ($idx === -1) die(json_encode(["status"=>"error"]));

// 4. JUSTE METTRE À JOUR LE STATUT (On ne touche pas aux images !)
// On garde 'alerte' = true, mais on ajoute 'handled_by'
$db['clients'][$idx]['handled_by'] = $user;
$db['clients'][$idx]['handled_at'] = date("H:i");

file_put_contents($json_file, json_encode($db, JSON_PRETTY_PRINT));

echo json_encode(["status"=>"success"]);
?>
