<?php
require_once __DIR__ . '/../bootstrap.php';

use Gestoreasy\Core\Http\Request;
use Gestoreasy\Core\Tenant\ConnectionManager;
use Gestoreasy\Core\Fiscal\TaxConfig;
use Gestoreasy\Core\Fiscal\NfceService;

function assertTrue($cond, $msg) { if (!$cond) { echo "FAIL: $msg\n"; exit(1); } }
function assertEq($a, $b, $msg) { if ($a !== $b) { echo "FAIL: $msg (".var_export($a,true)." !== ".var_export($b,true).")\n"; exit(1); } }

$pdo = ConnectionManager::forTenant();

// Configurar tenant para modelo 2026 com taxas de IBS/CBS/IS
$options = [ 'ibs_rate' => 0.18, 'cbs_rate' => 0.12, 'is_rate' => 0.05 ];
TaxConfig::save($pdo, '2026', null, 'homolog', $options);

// Itens de teste
$items = [
  [ 'descricao' => 'Produto A', 'quantidade' => 2, 'preco' => 50.00 ],
  [ 'descricao' => 'Produto B', 'quantidade' => 1, 'preco' => 100.00 ]
];

$headers = ['x-tenant-id' => 'default'];
$req = new Request($headers, [], ['items' => $items], [], 'POST', '/api/fiscal/nfce/autorizar');
$resp = NfceService::authorizeMockRJ($req, 'SALE-2026-TEST');

assertEq($resp['status'], 'authorized', 'Autorização retornou status authorized');
assertEq($resp['model_version'], '2026', 'model_version=2026 na resposta');

$xml = (string)$resp['xml'];
assertTrue(strpos($xml, '<versao>2026</versao>') !== false, 'XML inclui versao 2026');
assertTrue(strpos($xml, '<ibs>') !== false, 'XML inclui tag IBS');
assertTrue(strpos($xml, '<cbs>') !== false, 'XML inclui tag CBS');
assertTrue(strpos($xml, '<is>') !== false, 'XML inclui tag IS');

echo "PASS: NfceAuthorize2026Test (autoriza NFC-e com XML 2026 IBS/CBS/IS).\n";