<?php
require_once("_guard.php");
$body=json_decode(file_get_contents('php://input'), true) ?: [];
$userId=intval($body['user_id'] ?? 0);
$status=$body['status'] ?? '';
$reason=trim($body['rejection_reason'] ?? '');
if (!$userId || !in_array($status,['approved','rejected'],true)) {
  http_response_code(422); echo json_encode(['success'=>false,'message'=>'Invalid verification action']); exit();
}
if ($status === 'rejected' && $reason === '') {
  http_response_code(422); echo json_encode(['success'=>false,'message'=>'Give the trader a rejection reason']); exit();
}
$stmt=$conn->prepare("UPDATE user_verifications SET status=?, rejection_reason=? WHERE user_id=?");
$savedReason=$status === 'rejected' ? $reason : null;
$stmt->bind_param('ssi',$status,$savedReason,$userId);
$stmt->execute();
if ($stmt->affected_rows < 1) { http_response_code(404); echo json_encode(['success'=>false,'message'=>'Verification was not found']); exit(); }
echo json_encode(['success'=>true,'status'=>$status]);
?>
