<?php
require_once __DIR__ . '/../bootstrap.php';
use Gestoreasy\Core\Http\Request; use Gestoreasy\Core\Kernel; use Gestoreasy\Core\Auth\Jwt;

$kernel = new Kernel();
$token = Jwt::sign([ 'sub' => 1, 'email' => 'admin@local', 'tenant' => 'default', 'roles' => ['admin'] ]);
$H = ['x-tenant-id' => 'default', 'authorization' => 'Bearer ' . $token];

function p($label, $res){
    echo "== $label ==\n";
    echo "status: ".$res->status."\n";
    $b = $res->body; if (is_array($b)) { echo json_encode($b, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)."\n"; } else { var_dump($b); }
}

// Cria cliente e produto rápido
$resC = $kernel->handle(new Request($H, [], [ 'tipo'=>'PF','cpf'=>'529.982.247-25','nome_fantasia'=>'Debug C','razao_social'=>'Debug C Ltda','ie'=>'ISENTO','estado'=>'SP' ], [], 'POST', '/api/clientes'));
$cid = (int)($resC->body['id'] ?? 0);
$resP = $kernel->handle(new Request($H, [], [ 'codigo'=>'SKU-DBG-1','ncm'=>'12345678','cfop'=>'5102','preco'=>100 ], [], 'POST', '/api/produtos'));
$pid = (int)($resP->body['id'] ?? 0);

// Cria orçamento simples
$orc = [ 'cliente_id'=>$cid, 'items'=>[ ['product_id'=>$pid,'quantidade'=>1] ], 'pagamento'=>[], 'entrega'=>['frete'=>0,'frete_incluir_total'=>false] ];
$resOrc = $kernel->handle(new Request($H, [], $orc, [], 'POST', '/api/vendas/orcamentos'));
p('create_quote', $resOrc);
$qid = (int)($resOrc->body['id'] ?? 0);

// Tenta converter
$resConv = $kernel->handle(new Request($H, [], [], [], 'POST', "/api/vendas/orcamentos/$qid/converter"));
p('convert_quote', $resConv);