<?php
$pageTitle='Compare Rates'; require __DIR__.'/../blocks/app-header.php';
$from=preg_replace('/[^A-Z]/','',strtoupper(isset($_GET['from'])?$_GET['from']:'USD')); $to=preg_replace('/[^A-Z]/','',strtoupper(isset($_GET['to'])?$_GET['to']:'NGN')); $amount=max(1,(float)(isset($_GET['amount'])?$_GET['amount']:1000)); $mode=isset($_GET['mode'])?$_GET['mode']:'all';
$rates=db_all("SELECT r.*, s.name source_name, s.source_type, s.website_url FROM rate_quotes r JOIN rate_sources s ON s.id=r.source_id WHERE r.from_currency=? AND r.to_currency=? AND r.status='approved' AND r.expires_at>NOW() ORDER BY r.recipient_amount DESC",array($from,$to));
if(!$rates){$base=array('NGN'=>1398,'GHS'=>15.4,'KES'=>129.2,'UGX'=>3725,'TZS'=>2580,'ZAR'=>18.1,'XOF'=>606,'XAF'=>606,'RWF'=>1410,'ETB'=>132,'EGP'=>50.6,'MAD'=>9.7);$b=isset($base[$to])?$base[$to]:1;$names=array(array('Wise','app',.998,4.25),array('Remitly','app',.995,2.99),array('Taptap Send','app',1.002,0),array('FUNDZ Markets','provider',1.006,0));foreach($names as $i=>$n){$rate=$b*$n[2];$rates[]=array('source_name'=>$n[0],'source_type'=>$n[1],'rate'=>$rate,'fee_amount'=>$n[3],'recipient_amount'=>($amount-$n[3])*$rate,'delivery_label'=>$i===3?'Quote response':'Minutes–hours','captured_at'=>date('Y-m-d H:i:s'),'website_url'=>'#');}}
?><section class="container"><div class="section-head"><div><div class="eyebrow">MARKET COMPARISON</div><h2><?=e($from)?> to <?=e($to)?> for <?=number_format($amount,2)?></h2><p class="muted">Compare the amount received after fees. Quotes are indicative and may change before completion.</p></div><a class="orange-btn" href="<?=url('alerts/')?>">Create alert</a></div><div class="card" style="padding:0;overflow:hidden"><table class="comparison-table"><thead><tr><th>Provider</th><th>Type</th><th>Rate</th><th>Fee</th><th>Recipient gets</th><th>Speed</th><th>Updated</th></tr></thead><tbody><?php foreach($rates as $r): if($mode==='apps'&&$r['source_type']!=='app')continue;if($mode==='providers'&&$r['source_type']!=='provider')continue; ?><tr><td><strong><?=e($r['source_name'])?></strong></td><td><span class="badge <?=$r['source_type']==='provider'?'orange':''?>"><?=e($r['source_type'])?></span></td><td><?=number_format((float)$r['rate'],4)?></td><td><?=number_format((float)$r['fee_amount'],2).' '.e($from)?></td><td><strong><?=number_format((float)$r['recipient_amount'],2).' '.e($to)?></strong></td><td><?=e($r['delivery_label'])?></td><td><?=e($r['captured_at'])?></td></tr><?php endforeach; ?></tbody></table></div></section><?php require __DIR__.'/../blocks/app-footer.php'; ?>