<?php
namespace Gestoreasy\Core\Fiscal2026;

class Calculator2026 {
    // Mock de cálculo 2026: IBS/CBS/IS ilustrativos para preview
    public function previewNfce(array $items, array $opts = []): array {
        $cfg = $opts['config'] ?? ['model_version' => '2026', 'options' => []];
        $ibsRate = (float)($cfg['options']['ibs_rate'] ?? 0.18);
        $cbsRate = (float)($cfg['options']['cbs_rate'] ?? 0.12);
        $isRate  = (float)($cfg['options']['is_rate']  ?? 0.00); // IS pode não incidir; padrão 0

        $normalized = [];
        $subtotal = 0.0;
        $ibsTotal = 0.0;
        $cbsTotal = 0.0;
        $isTotal  = 0.0;
        foreach ($items as $i) {
            $q = (float)($i['quantidade'] ?? 1);
            $p = (float)($i['preco'] ?? 0);
            $total = $q * $p;
            $ibs = round($total * $ibsRate, 2);
            $cbs = round($total * $cbsRate, 2);
            $is  = round($total * $isRate, 2);
            $normalized[] = [
                'descricao' => (string)($i['descricao'] ?? 'Item'),
                'quantidade' => $q,
                'preco' => $p,
                'total' => $total,
                'ibs' => ['rate' => $ibsRate, 'valor' => $ibs],
                'cbs' => ['rate' => $cbsRate, 'valor' => $cbs],
                'is'  => ['rate' => $isRate,  'valor' => $is],
            ];
            $subtotal += $total;
            $ibsTotal += $ibs;
            $cbsTotal += $cbs;
            $isTotal  += $is;
        }
        return [
            'items' => $normalized,
            'subtotal' => round($subtotal, 2),
            'ibs_total' => round($ibsTotal, 2),
            'cbs_total' => round($cbsTotal, 2),
            'is_total'  => round($isTotal, 2),
            'total' => round($subtotal + $ibsTotal + $cbsTotal + $isTotal, 2),
            'model_version' => '2026'
        ];
    }
}