<?php

use App\Models\Invoice;
use Illuminate\Contracts\Console\Kernel;

require __DIR__.'/../../vendor/autoload.php';

$app = require __DIR__.'/../../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();

$id = $argv[1] ?? '';

$invoice = Invoice::with(['asset', 'address', 'invoiceTransactions.chainTransaction'])
    ->where('public_id', $id)
    ->first();

if (! $invoice) {
    fwrite(STDERR, "Invoice not found: {$id}\n");
    exit(1);
}

echo json_encode([
    'invoice_id' => $invoice->public_id,
    'status' => $invoice->status,
    'pay_currency' => $invoice->asset?->code,
    'network' => $invoice->asset?->network,
    'pay_address' => $invoice->address?->address,
    'paid_amount_atomic' => $invoice->paid_amount_atomic,
    'transactions' => $invoice->invoiceTransactions->map(fn ($transaction) => [
        'amount_atomic' => $transaction->amount_atomic,
        'confirmations' => $transaction->confirmations,
        'tx_hash' => $transaction->chainTransaction?->tx_hash,
        'status' => $transaction->chainTransaction?->status,
    ])->values(),
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES).PHP_EOL;
