use Suot\Fiscal\Core\Exception\InfrastructureException;
use Suot\Fiscal\Core\Result\RetryMode;
use Suot\Fiscal\Core\ValueObject\Uf;
use Suot\Fiscal\Bpe\Data\BpeAddress;
use Suot\Fiscal\Bpe\Data\BpeEmitter;
use Suot\Fiscal\Bpe\Data\BpeMunicipality;
use Suot\Fiscal\Bpe\Data\BpePayment;
use Suot\Fiscal\Bpe\Data\BpeRoute;
use Suot\Fiscal\Bpe\Data\BpeTaIbsCbs;
use Suot\Fiscal\Bpe\Data\BpeTaPassage;
use Suot\Fiscal\Bpe\Data\BpeTaTrip;
use Suot\Fiscal\Bpe\Data\BpeTaValues;
use Suot\Fiscal\Bpe\Data\BpeValueComponent;
$palmas = new BpeMunicipality('1721000', 'Palmas', 'TO');
$saoPaulo = new BpeMunicipality('3550308', 'Sao Paulo', 'SP');
try {
$result = $bpeTa->issue()
->forOperation('flight-batch-42-issue')
->inHomologation()
->issuedIn(new Uf('TO'))
->numbered('1', '42')
->purchaseType('0')
->buyerPresence('1')
->followingRoute(new BpeRoute($palmas, $saoPaulo))
->issuedBy(new BpeEmitter(
cnpj: '60708000114',
ie: '0961234567',
name: 'BPE TA EMITIDO EM AMBIENTE DE HOMOLOGACAO - SEM VALOR FISCAL',
tradeName: 'CIA AEREA HOMOLOG',
address: new BpeAddress(
street: 'Rua Teste',
number: '100',
district: 'Centro',
zip: '77000000',
municipality: $palmas,
),
))
->withPassage(new BpeTaPassage(
boardingAt: '2026-07-15T14:00:00-03:00',
validUntil: '2026-07-15T23:59:59-03:00',
))
->withTrip(new BpeTaTrip(
flightNumber: 'G31234',
operatingAirlineIata: 'G3',
tripType: '00',
originAirportIata: 'PMW',
destinationAirportIata: 'GRU',
serviceType: '12',
accommodationType: '6',
segmentType: '1',
travelAt: '2026-07-15T14:00:00-03:00',
))
->withValues(new BpeTaValues(
ticketValue: '150.00',
discount: '0.00',
paymentValue: '150.00',
change: '0.00',
components: [new BpeValueComponent('01', '150.00')],
))
->withIbsCbs(new BpeTaIbsCbs(
cst: '000',
taxClassification: '000001',
))
->withTotalDfeValue('150.00')
->withPayment(new BpePayment(type: '01', amount: '150.00'))
->handle();
} catch (InfrastructureException $e) {
$failure = $e->toFailure('issue');
// Host: alertar; filar retry conforme $failure->retryPolicy (timeout ambíguo → QueryBeforeRetry)
throw $e;
}
if ($result->isAuthorized()) {
// Host: persistir XML, protocolo, chave; não reemitir o mesmo operationId
} elseif ($result->isRejected()) {
$error = $result->failure();
// Host: corrigir dados ou cadastro; não reenviar cego
} elseif ($result->isProcessing()) {
$policy = $result->retryPolicy();
// Host: consultar por chave antes de retry se RetryMode exigir
}