<?php
require_once __DIR__.'/../../config.php';
require_once __DIR__ . '/../../engine/Kernel.php';
require_once __DIR__.'/../../engine/Logger.php';
require_once __DIR__.'/../../engine/QRPayEngine.php';
if ($_SERVER['REQUEST_METHOD']!=='POST'){http_response_code(405);exit;}
$ctx = Kernel::handle(['auth' => true]);
$u   = $ctx['user'];
$b = json_decode(file_get_contents('php://input'),true)??[];
$type   = in_array($b['type']??'',['static','dynamic','merchant'])?$b['type']:'dynamic';
$amount = isset($b['amount_sdg'])?(float)$b['amount_sdg']:null;
$result = $type==='static'
  ? QRPayEngine::generateMerchantStatic((int)$u['id'], $u['name']??'')
  : QRPayEngine::generate((int)$u['id'], $amount);
echo json_encode($result, JSON_UNESCAPED_UNICODE);
