<?php
header('Content-Type: application/json; charset=utf-8');

$placa = "PQA4703";
$fingerprint = "t9vF262pEP0uG0oFnk5q";
$terminal = "4068707027";
$proxy = "gate.nodemaven.com:8080";
$proxyAuth = "pkscabelim_gmail_com-country-br-ipv4-true:xcy2s9jsob";

$commonHeaders = [
    "terminal: {$terminal}",
    "x-fingerprint-site-id: {$fingerprint}",
    "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
    "origin: https://www.zignet.com.br",
    "referer: https://www.zignet.com.br/selecao-debitos",
    "accept: application/json, text/plain, */*"
];

// 1. CONSULTA VEÍCULO
$urlVehicle = "https://nfbti53lkd.execute-api.us-east-2.amazonaws.com/dev/site/vehicle/{$placa}?plate={$placa}&email=rickcooder@gmail.com&phone=62996494518&uf=GO&fingerprintId={$fingerprint}";
$ch = curl_init($urlVehicle);
curl_setopt($ch, CURLOPT_HTTPHEADER, $commonHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$res1 = curl_exec($ch);
$data1 = json_decode($res1, true);
curl_close($ch);

if (!isset($data1['vehicle']['id'])) {
    echo json_encode(["error" => "Falha no passo 1", "response" => $data1]);
    exit;
}

$vehicleId = $data1['vehicle']['id'];

// 2. CONSULTA DÉBITOS (IMEDIATO)
$urlDebts = "https://nfbti53lkd.execute-api.us-east-2.amazonaws.com/dev/site/debts/{$vehicleId}?document=&renavam=482447&chassi=&uf=GO&fingerprintId={$fingerprint}";
$ch = curl_init($urlDebts);
curl_setopt($ch, CURLOPT_HTTPHEADER, $commonHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyAuth);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$res2 = curl_exec($ch);
$data2 = json_decode($res2, true);
curl_close($ch);

echo json_encode([
    "step1_vehicle" => $data1,
    "step2_debts" => $data2
], JSON_PRETTY_PRINT);
