<?php

use App\Application\Services\Blockchain\Trc20TransactionProcessor;
use App\Domain\Blockchain\Contracts\BlockchainGateway;
use App\Jobs\PollOpenInvoiceAddressJob;
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();

$publicId = $argv[1] ?? '';
$invoice = Invoice::where('public_id', $publicId)->first();

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

(new PollOpenInvoiceAddressJob($invoice->id))->handle(
    app(BlockchainGateway::class),
    app(Trc20TransactionProcessor::class)
);

echo "Polled invoice {$publicId}\n";
