<?php
namespace App\Jobs;
use App\Domain\Blockchain\Contracts\BlockchainGateway;
use App\Models\ChainTransaction;
use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels;
final class RefreshTransactionFinalityJob implements ShouldQueue
{
 use Dispatchable,InteractsWithQueue,Queueable,SerializesModels; public int $tries=20; public function __construct(public int $transactionId){$this->onQueue('confirmations');}
 public function handle(BlockchainGateway $gateway):void { $tx=ChainTransaction::with('invoiceLinks.invoice')->findOrFail($this->transactionId);$fresh=$gateway->getTransaction($tx->network,$tx->tx_hash);$tx->update(['confirmations'=>$fresh->confirmations,'finality_status'=>$fresh->status,'block_height'=>$fresh->blockHeight]);$asset=$tx->asset;$policy=$asset->confirmation_policy??[];$required=(int)($policy['minimum_confirmations']??1);if($fresh->status==='confirmed'&&$fresh->confirmations>=$required){foreach($tx->invoiceLinks as $link)FinalizePaymentJob::dispatch($link->invoice_id)->onQueue('confirmations');return;}$delay=(int)($policy['recheck_seconds']??10);self::dispatch($tx->id)->delay(now()->addSeconds($delay))->onQueue('confirmations'); }
}
