<?php
require_once __DIR__ . '/_super_admin_common.php';

$body = sa_require_post_json();
[$uid, $email, $dbUrl, $accessToken] = sa_require_super_admin_session_with_db();

$clientId = trim((string)($body['clientId'] ?? ''));
$status = trim((string)($body['status'] ?? ''));
if ($clientId === '' || !in_array($status, ['active', 'suspended'], true)) {
    sa_json_response(400, ['status' => 'error', 'message' => 'clientId and valid status required']);
}

$patch = firebase_rtdb_patch($dbUrl, 'clients/' . $clientId, $accessToken, [
    'status' => $status,
    'updatedAt' => time() * 1000,
]);
if (isset($patch['error'])) sa_json_response(500, ['status' => 'error', 'message' => 'Could not update client']);

sa_audit_log($dbUrl, $accessToken, $uid, $email, 'update_client_status', $clientId, ['status' => $status]);
sa_json_response(200, ['status' => 'success', 'message' => 'Client updated successfully']);
