<?php
// Porte fiel de api/pix/status.php
require_once __DIR__ . '/../config.php';

$MAP = [
    'PAID' => 'paid',
    'PENDING' => 'pending',
    'REFUNDED' => 'refunded',
    'REFUSED' => 'refused',
    'FAILED' => 'refused',
    'EXPIRED' => 'expired',
    'CHARGEBACK' => 'refunded',
    'PRECHARGEBACK' => 'pending',
    'ERROR' => 'refused',
];

$id = $_GET['transaction_id'] ?? '';
if (!preg_match('/^[\w-]{1,64}$/', $id)) json_out(['status' => 'unknown'], 400);

$res = vennox_request('/v1/payment-transaction/info/' . $id);

if ($res['status'] < 200 || $res['status'] >= 300) {
    error_log("Vennox status falhou [{$res['status']}]: " . $res['raw']);
    json_out(['status' => 'pending']);
}

$info = vennox_unwrap($res['json']);
$rawStatus = strtoupper((string) ($info['status'] ?? ''));

$out = ['status' => $MAP[$rawStatus] ?? 'pending'];
if (isset($info['amount']) && is_numeric($info['amount'])) {
    $out['amount'] = ((float) $info['amount']) / 100;
}

json_out($out);
