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

use Gestoreasy\Core\Fiscal\VersionRouter;

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

// Itens básicos para preview
$items = [ [ 'descricao' => 'Item Teste', 'quantidade' => 2, 'preco' => 50.00 ] ];
$preview = VersionRouter::previewNfce($items, '2026');

assertTrue(is_array($preview), 'Preview deve ser array');
assertEquals('2026', ($preview['model_version'] ?? ''), 'model_version deve ser 2026');
assertTrue(isset($preview['ibs_total']), 'Preview inclui ibs_total');
assertTrue(isset($preview['cbs_total']), 'Preview inclui cbs_total');
assertTrue(isset($preview['is_total']), 'Preview inclui is_total');
assertTrue(isset($preview['subtotal']), 'Preview inclui subtotal');
assertTrue(isset($preview['total']), 'Preview inclui total');

// Verificar consistência do total: subtotal + IBS + CBS + IS
$calcTotal = round((float)$preview['subtotal'] + (float)$preview['ibs_total'] + (float)$preview['cbs_total'] + (float)$preview['is_total'], 2);
assertEquals($calcTotal, (float)$preview['total'], 'Total consistente com soma dos tributos');

echo "PASS: Fiscal2026PreviewTest (IBS/CBS/IS, totais segregados e consistência).\n";