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

use Gestoreasy\Core\Validation\Br;

function computePrice(float $compra, float $descV = 0.0, float $descP = 0.0, float $stV = 0.0, float $stP = 0.0, float $ipiV = 0.0, float $ipiP = 0.0, float $fcpV = 0.0, float $pisCofP = 0.0, float $frete = 0.0, float $seguro = 0.0, float $outros = 0.0, float $markup = 0.0): array {
    $desconto = $descV > 0 ? $descV : ($descP > 0 ? $compra * ($descP / 100.0) : 0.0);
    $st = $stV > 0 ? $stV : ($stP > 0 ? $compra * ($stP / 100.0) : 0.0);
    $ipi = $ipiV > 0 ? $ipiV : ($ipiP > 0 ? $compra * ($ipiP / 100.0) : 0.0);
    $pisCof = $pisCofP > 0 ? $compra * ($pisCofP / 100.0) : 0.0;
    $base = $compra - $desconto;
    $custoFinal = $base + $st + $ipi + $fcpV + $frete + $seguro + $outros + $pisCof;
    $precoVenda = $custoFinal * (1.0 + ($markup / 100.0));
    return compact('compra','desconto','base','st','ipi','fcpV','frete','seguro','outros','pisCof','custoFinal','markup','precoVenda');
}

// Teste 1: cálculo simples
$r1 = computePrice(100.0, 0.0, 0.0, 0.0, 0.0, 10.0, 0.0, 0.0, 9.65, 5.0, 0.0, 0.0, 20.0);
assert(round($r1['custoFinal'], 2) === round(100.0 + 10.0 + (100.0 * 0.0965) + 5.0, 2));
assert(round($r1['precoVenda'], 2) === round($r1['custoFinal'] * 1.2, 2));

// Teste 2: desconto percentual e ST %
$r2 = computePrice(200.0, 0.0, 5.0, 0.0, 8.0, 0.0, 0.0, 0.0, 0.0, 20.0, 2.0, 3.0, 30.0);
assert(round($r2['desconto'], 2) === 10.0);
assert(round($r2['st'], 2) === 16.0);
assert(round($r2['custoFinal'], 2) === round((200.0 - 10.0) + 16.0 + 20.0 + 2.0 + 3.0, 2));

// Teste 3: validações BR
assert(Br::isNCM('61091000') === true);
assert(Br::isCFOP('5102') === true);
assert(Br::isEAN('7891234567890') === true);

echo "ProductsPriceCalcTest: OK\n";