<?php
 $user = "swiftcar_westbaytrusts";
 $pass = "softproweb1";
 $servername = "localhost";
 $dbname = "swiftcar_westbaytrusts";

$connect = new mysqli("localhost", "swiftcar_westbaytrusts", "softproweb1", "swiftcar_westbaytrusts");
$conn = $connect;
if (mysqli_connect_errno())
{
   printf("Connect failed: %s", mysqli_connect_error());
   exit();
}



function getUserDetails($det, $str) {
  global $connect;
  $stmt = $connect->prepare("SELECT * FROM users WHERE email = ? LIMIT 1");
  $stmt->bind_param("s", $str);
  $stmt->execute();
  $row = $stmt->get_result()->fetch_assoc();
  $stmt->close();
  return $row[$det] ?? null;
}

function currencySymbol($code) {
  $s = ['AED'=>'د.إ','AFN'=>'Af','ALL'=>'Lek','ANG'=>'ƒ','AOA'=>'Kz','ARS'=>'$','AUD'=>'$','AWG'=>'ƒ','AZN'=>'ман','BAM'=>'KM','BBD'=>'$','BDT'=>'৳','BGN'=>'лв','BHD'=>'.د.ب','BIF'=>'FBu','BMD'=>'$','BND'=>'$','BOB'=>'$b','BRL'=>'R$','BTC'=>'₿','BSD'=>'$','BTN'=>'Nu.','BWP'=>'P','BYR'=>'p.','BZD'=>'BZ$','CAD'=>'$','CDF'=>'FC','CHF'=>'CHF','CLP'=>'$','CNY'=>'¥','COP'=>'$','CRC'=>'₡','CUP'=>'⃌','CVE'=>'$','CZK'=>'Kč','DJF'=>'Fdj','DKK'=>'kr','DOP'=>'RD$','DZD'=>'دج','EGP'=>'£','ETB'=>'Br','ETH'=>'ETH','EUR'=>'€','FJD'=>'$','FKP'=>'£','GBP'=>'£','GEL'=>'ლ','GHS'=>'¢','GIP'=>'£','GMD'=>'D','GNF'=>'FG','GTQ'=>'Q','GYD'=>'$','HKD'=>'$','HNL'=>'L','HRK'=>'kn','HTG'=>'G','HUF'=>'Ft','IDR'=>'Rp','ILS'=>'₪','INR'=>'₹','IQD'=>'ع.د','IRR'=>'﷼','ISK'=>'kr','JEP'=>'£','JMD'=>'J$','JOD'=>'JD','JPY'=>'¥','KES'=>'KSh','KGS'=>'лв','KHR'=>'៛','KMF'=>'CF','KPW'=>'₩','KRW'=>'₩','KWD'=>'د.ك','KYD'=>'$','KZT'=>'лв','LAK'=>'₭','LBP'=>'£','LKR'=>'₨','LRD'=>'$','LSL'=>'L','LTL'=>'Lt','LVL'=>'Ls','LYD'=>'ل.د','MAD'=>'د.م.','MDL'=>'L','MGA'=>'Ar','MKD'=>'ден','MMK'=>'K','MNT'=>'₮','MOP'=>'MOP$','MRO'=>'UM','MUR'=>'₨','MVR'=>'.ރ','MWK'=>'MK','MXN'=>'$','MYR'=>'RM','MZN'=>'MT','NAD'=>'$','NGN'=>'₦','NIO'=>'C$','NOK'=>'kr','NPR'=>'₨','NZD'=>'$','OMR'=>'﷼','PAB'=>'B/.','PEN'=>'S/.','PGK'=>'K','PHP'=>'₱','PKR'=>'₨','PLN'=>'zł','PYG'=>'Gs','QAR'=>'﷼','RON'=>'lei','RSD'=>'Дин.','RUB'=>'руб','RWF'=>'ر.س','SAR'=>'﷼','SBD'=>'$','SCR'=>'₨','SDG'=>'£','SEK'=>'kr','SGD'=>'$','SHP'=>'£','SLL'=>'Le','SOS'=>'S','SRD'=>'$','STD'=>'Db','SVC'=>'$','SYP'=>'£','SZL'=>'L','THB'=>'฿','TJS'=>'TJS','TMT'=>'m','TND'=>'د.ت','TOP'=>'T$','TRY'=>'₤','TTD'=>'$','TWD'=>'NT$','UAH'=>'₴','UGX'=>'USh','USD'=>'$','UYU'=>'$U','UZS'=>'лв','VEF'=>'Bs','VND'=>'₫','VUV'=>'VT','WST'=>'WS$','XAF'=>'FCFA','XCD'=>'$','XPF'=>'F','YER'=>'﷼','ZAR'=>'R','ZMK'=>'ZK','ZWL'=>'Z$'];
  return $s[$code] ?? $code;
}

function getBtcPrice($currCode) {
  $currCode = strtolower($currCode ?: 'usd');
  $cacheKey = 'btc_price_' . $currCode;
  if (!isset($_SESSION[$cacheKey]) || (time() - ($_SESSION[$cacheKey . '_ts'] ?? 0)) > 300) {
    $ch = curl_init("https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies={$currCode}");
    curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_USERAGENT => 'Mozilla/5.0']);
    $raw = curl_exec($ch);
    curl_close($ch);
    if ($raw) {
      $price = json_decode($raw, true)['bitcoin'][$currCode] ?? 0;
      if ($price > 0) { $_SESSION[$cacheKey] = $price; $_SESSION[$cacheKey . '_ts'] = time(); }
    }
  }
  return $_SESSION[$cacheKey] ?? 0;
}

function getSiteConfig($str) {
  global $connect;
  $res = $connect->query("SELECT * FROM siteconfig WHERE id = '1'");
  $row = $res ? $res->fetch_assoc() : [];
  return $row[$str] ?? null;
}

function sendOtp($email, $amount = 0) {
    global $connect;
    $otp      = (string)rand(100000, 999999);
    $stmt     = $connect->prepare("UPDATE users SET otp = ? WHERE email = ?");
    $stmt->bind_param("ss", $otp, $email);
    $stmt->execute();
    $stmt->close();

    $fullname = (string)(getUserDetails('fullname', $email) ?? 'Customer');
    $sitename = (string)(getSiteConfig('sitename') ?? 'CreditPay');
    $sitemail = (string)(getSiteConfig('sitemail') ?? '');
    $amt      = number_format((float)$amount, 2);

    $subject = "Your Transfer OTP — {$sitename}";
    $body    = "Dear {$fullname},\r\n\r\n"
             . "Your one-time password (OTP) to authorize your local transfer of {$amt} is:\r\n\r\n"
             . "        {$otp}\r\n\r\n"
             . "This code is valid for one use only. Do not share it with anyone.\r\n"
             . "If you did not request this, please contact support immediately.\r\n\r\n"
             . "— {$sitename} Security Team";
    $headers = "From: {$sitename} <{$sitemail}>\r\nContent-type: text/plain; charset=UTF-8";

    @mail($email, $subject, $body, $headers);
}


