<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

/**
 * Professional Responsive HTML Wrapper
 */
function getEmailTemplate($title, $bodyContent, $buttonText = "Log In to Dashboard") {
    return "
    <!DOCTYPE html>
    <html lang='en'>
    <head>
        <meta charset='UTF-8'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0'>
        <meta http-equiv='X-UA-Compatible' content='IE=edge'>
        <style>
            /* Mobile Specific Overrides */
            @media screen and (max-width: 600px) {
                .main-table { width: 100% !important; border-radius: 0 !important; }
                .content-padding { padding: 30px 20px !important; }
                .header-padding { padding: 20px !important; }
                .button { width: 100% !important; box-sizing: border-box !important; }
            }
        </style>
    </head>
    <body style='font-family: \"Segoe UI\", Roboto, Helvetica, Arial, sans-serif; background-color: #f8fafc; margin: 0; padding: 0; -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%;'>
        <div style='width: 100%; background-color: #f8fafc; padding: 20px 0;'>
            <table align='center' border='0' cellpadding='0' cellspacing='0' class='main-table' style='background-color: #ffffff; margin: 0 auto; width: 100%; max-width: 600px; border-radius: 8px; overflow: hidden; border-collapse: collapse; box-shadow: 0 4px 6px rgba(0,0,0,0.05);'>
                
                <tr>
                    <td class='header-padding' style='padding: 30px; border-bottom: 1px solid #f1f5f9; background-color: #ffffff;'>
                        <img src='https://nexusvault.site/home/images/logo.png' alt='Nexus Vault' style='height: 40px; width: auto; display: block; outline: none; border: none; text-decoration: none;'>
                    </td>
                </tr>

                <tr>
                    <td class='content-padding' style='padding: 40px 30px;'>
                        <h1 style='font-size: 22px; font-weight: 700; color: #1e293b; margin: 0 0 20px 0; line-height: 1.3;'>$title</h1>
                        
                        <div style='font-size: 16px; color: #64748b; line-height: 1.6; margin-bottom: 25px;'>
                            $bodyContent
                        </div>

                        <div style='padding-bottom: 30px; text-align: center;'>
                            <a class='button' href='https://nexusvault.site/home/' style='background-color: #1565d8; border-radius: 6px; color: #ffffff !important; display: inline-block; font-size: 16px; font-weight: 700; line-height: 50px; text-align: center; text-decoration: none; width: 220px; -webkit-text-size-adjust: none;'>$buttonText</a>
                        </div>

                        <div style='background-color: #f8fafc; border-left: 4px solid #1565d8; padding: 20px; border-radius: 4px;'>
                            <p style='font-size: 13px; color: #475569; margin: 0; line-height: 1.5;'>
                                <strong style='color: #1e293b;'>Security Tip:</strong> Nexus Vault will never ask for your 4-digit PIN or password via email. If you did not perform this action, please contact our support team immediately.
                            </p>
                        </div>
                    </td>
                </tr>
            </table>

            <table align='center' border='0' cellpadding='0' cellspacing='0' width='100%' style='max-width: 600px; margin: 0 auto; text-align: center;'>
                <tr>
                    <td style='padding: 30px;'>
                        <p style='font-size: 12px; color: #94a3b8; line-height: 1.8; margin: 0;'>
                            &copy; 2026 Nexus Vault International Group. All Rights Reserved.<br>
                            Regulated global financial institution serving high-net-worth clients across 120+ countries.<br>
                            <a href='#' style='color: #1565d8; text-decoration: none;'>Privacy Policy</a> | <a href='#' style='color: #1565d8; text-decoration: none;'>Support Center</a>
                        </p>
                    </td>
                </tr>
            </table>
        </div>
    </body>
    </html>";
}

/**
 * Core SMTP Mailer Function
 */
function sendSystemEmail($toEmail, $userName, $subject, $htmlContent) {
    $mail = new PHPMailer(true);
    try {
        $mail->isSMTP();
        $mail->Host       = 'mail.nexusvault.site'; 
        $mail->SMTPAuth   = true;
        $mail->Username   = 'noreply@nexusvault.site'; 
        $mail->Password   = 'Casper2017$$$';         
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port       = 587;

        $mail->setFrom('noreply@nexusvault.site', 'Nexus Vault');
        $mail->addAddress($toEmail, $userName);

        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $htmlContent;

        $mail->send();
        return true;
    } catch (Exception $e) {
        return false;
    }
}

/**
 * Transaction Alert Function
 */
function sendTransactionAlert($userId, $type, $amount, $description, $reference) {
    global $con;
    $query = mysqli_query($con, "SELECT fullname, email, balance, account_number FROM users WHERE id = '$userId'");
    $user = mysqli_fetch_assoc($query);

    if (!$user) return false;

    $balance = number_format($user['balance'], 2);
    $formattedAmount = number_format($amount, 2);
    $accNum = "****" . substr($user['account_number'], -4);

    $bodyContent = "
        <p style='margin: 0 0 15px 0;'>Dear {$user['fullname']},</p>
        <p style='margin: 0 0 20px 0;'>This is to notify you of a <strong style='color: #1e293b;'>" . strtoupper($type) . "</strong> request on account <strong>$accNum</strong>.</p>
        
        <table width='100%' border='0' cellpadding='0' cellspacing='0' style='background-color: #f8fafc; border-radius: 8px; margin-bottom: 10px;'>
            <tr>
                <td style='padding: 20px;'>
                    <table width='100%' border='0' cellpadding='0' cellspacing='0' style='font-size: 14px;'>
                        <tr>
                            <td style='padding: 5px 0; color: #64748b;'>Amount:</td>
                            <td align='right' style='color: #1e293b; font-weight: 700;'>USD $formattedAmount</td>
                        </tr>
                        <tr>
                            <td style='padding: 5px 0; color: #64748b;'>Status:</td>
                            <td align='right' style='color: #1565d8; font-weight: 700;'>Completed</td>
                        </tr>
                        <tr>
                            <td style='padding: 5px 0; color: #64748b;'>Reference:</td>
                            <td align='right' style='color: #1e293b;'>$reference</td>
                        </tr>
                        <tr>
                            <td style='padding: 10px 0 5px 0; border-top: 1px solid #e2e8f0; color: #64748b;'>Available Balance:</td>
                            <td align='right' style='padding: 10px 0 5px 0; border-top: 1px solid #e2e8f0; color: #1e293b; font-weight: 700;'>USD $balance</td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>";

    $fullMessage = getEmailTemplate("Transaction Notification", $bodyContent);
    return sendSystemEmail($user['email'], $user['fullname'], "Transaction Alert: " . ucfirst($type), $fullMessage);
}
?>
