<?php
require_once __DIR__ . '/../../includes/email.php';

$fullname = $firstname . ' ' . $lastname;
$token = bin2hex(random_bytes(32));
$mysqli->query("INSERT INTO email_confirmation (email, token, created_at) VALUES ('$email', '$token', NOW())");
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$siteUrl = $protocol . '://' . $_SERVER['HTTP_HOST'];
$confirmationLink = $siteUrl . "/secure/confirm-email.php?source=" . $token;
$created_at = date("F j, Y, g:i a");

$details = [
    'Account Name'     => $fullname,
    'Account Number'   => $new_acc_num,
    'Account Type'     => $account_type ?? 'Checking',
    'Account Currency' => $usercurrency ?? 'USD',
    'Date'             => $created_at,
];

try {
    sendBankEmail(
        $email,
        'Welcome — Account Registration',
        $fullname,
        'Account Registration — Confirmation Required',
        '<p>Your account profile has been created successfully. Before you can access our internet banking service, please verify your email address by clicking the button below.</p>
        <p style="font-size:13px;color:#888;">Privacy is paramount at our bank. All data we handle is encrypted and protected — never shared with third parties.</p>',
        'Confirm Email Address',
        $confirmationLink,
        $details,
        '<div style="padding:0 40px 16px 40px;"><p style="font-size:12px;word-break:break-all;color:#0a1f44;">Or copy this link: ' . htmlspecialchars($confirmationLink) . '</p></div>'
    );
} catch (Exception $e) {
    error_log('Email send failed: ' . $e->getMessage());
}
