<?php
namespace App\Jobs;
use App\Application\Services\Blockchain\Trc20TransactionProcessor;
use App\Domain\Blockchain\Contracts\BlockchainGateway;
use App\Domain\Blockchain\Data\AddressCursor;
use App\Models\{Asset,Invoice};
use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\Middleware\RateLimited; use Illuminate\Queue\SerializesModels;
final class PollOpenInvoiceAddressJob implements ShouldQueue
{
 use Dispatchable,InteractsWithQueue,Queueable,SerializesModels; public int $tries=8; public function __construct(public int $invoiceId){$this->onQueue('reconciliation');}
 public function middleware():array{return [new RateLimited('blockchain-provider')];}
 public function handle(BlockchainGateway $gateway,Trc20TransactionProcessor $processor):void { $invoice=Invoice::with(['address','asset'])->find($this->invoiceId);if(!$invoice||!in_array($invoice->status,['waiting','confirming','underpaid'],true)||!$invoice->address)return;$cursor=null;do{$page=$gateway->listAddressTransactions(new AddressCursor($invoice->asset->network,$invoice->address->address,$cursor,$invoice->asset->contract_address,100));foreach($page->transactions as $tx)$processor->process($tx,$invoice->asset);$cursor=$page->nextCursor;}while($cursor!==null); }
}
