<?php

/**
 * =========================================
 * DEMOBANK MAILER SYSTEM (PROFESSIONAL VERSION)
 * =========================================
 */

require_once __DIR__ . "/functions.php";

/**
 * =========================================
 * BASE EMAIL SENDER
 * =========================================
 */
function sendMail($to, $subject, $message) {

    $fromEmail = "no-reply@texascapitalbg.com";
    $fromName  = "Texas Capital Bank";

    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8\r\n";
    $headers .= "From: $fromName <$fromEmail>\r\n";
    $headers .= "Reply-To: info@texascapitalbg.com\r\n";
    $headers .= "Return-Path: $fromEmail\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";

    $body = "
    <html>
    <body style='font-family:Arial, sans-serif; background:#f4f6f9; margin:0; padding:20px;'>
        <div style='max-width:600px; margin:auto; background:#ffffff; border-radius:8px; overflow:hidden; box-shadow:0 5px 15px rgba(0,0,0,0.1);'>

            <div style='background:#1a73e8; color:#fff; padding:20px; text-align:center;'>
                <h2 style='margin:0;'>Texas Capital Bank Notification</h2>
            </div>

            <div style='padding:25px; color:#333; font-size:16px; line-height:1.6;'>
                $message
            </div>

            <div style='background:#f1f1f1; color:#777; text-align:center; padding:15px; font-size:12px;'>
                This is an automated message from Texas Capital Bank. Please do not reply directly to this email.<br>
                Visit <a href='https://web.texascapitalbg.com' style='color:#1a73e8;'>Texas Capital Bank</a> for support.
            </div>

        </div>
    </body>
    </html>
    ";

    $sent = mail($to, $subject, $body, $headers);

    // LOGGING
    if ($sent) {
        file_put_contents(
            __DIR__ . '/../logs/mail.log',
            date("Y-m-d H:i:s") . " | SUCCESS | $to | $subject\n",
            FILE_APPEND
        );
        return true;
    } else {
        file_put_contents(
            __DIR__ . '/../logs/mail_error.log',
            date("Y-m-d H:i:s") . " | FAILED | $to | $subject\n",
            FILE_APPEND
        );
        return false;
    }
}

/**
 * =========================================
 * USER CREATED EMAIL (ADMIN)
 * =========================================
 */
function sendAccountCreatedEmail($email, $name, $accountNo, $password = null) {

    $subject = "Welcome to Texas Capital Bank - Account Details";

    $message = "
        <p>Hello <b>$name</b>,</p>
        <p>Your Texas Capital Bank account has been successfully created.</p>

        <div style='background:#eef2ff; padding:15px; border-radius:6px; margin:15px 0;'>
            <p><b>Account Number:</b> $accountNo</p>
            <p><b>Login Email:</b> $email</p>
            " . ($password ? "<p><b>Temporary Password:</b> $password</p>" : "") . "
        </div>

        <p style='color:#b00020;'>⚠ Please change your password after your first login for security reasons.</p>

        <p>
            <a href='https://web.texascapitalbg.com/auth/login.php'
               style='display:inline-block; background:#1a73e8; color:#fff; padding:12px 25px; text-decoration:none; border-radius:5px;'>
               Login to Texas Capital Bank
            </a>
        </p>
    ";

    return sendMail($email, $subject, $message);
}

/**
 * =========================================
 * PASSWORD RESET EMAIL
 * =========================================
 */
function sendPasswordResetEmail($email, $token) {

    $link = "https://web.texascapitalbg.com/auth/reset_password.php?token=$token";
    $subject = "Texas Capital Bank Password Reset Request";

    $message = "
        <p>Hello,</p>
        <p>We received a request to reset your Texas Capital Bank password. Click the button below to securely reset it:</p>
        
        <p style='text-align:center; margin:20px 0;'>
            <a href='$link'
               style='display:inline-block; background:#1a73e8; color:#fff; padding:12px 25px; text-decoration:none; border-radius:5px;'>
               Reset Password
            </a>
        </p>

        <p style='color:#555; font-size:14px;'>If you did not request this, you can safely ignore this email.</p>
    ";

    return sendMail($email, $subject, $message);
}

/**
 * =========================================
 * AUTO LOGIN EMAIL
 * =========================================
 */
function sendAutoLoginEmail($email, $token, $first_name, $last_name) {

    $full_name = trim($first_name . ' ' . $last_name);

    // ✅ ADD THIS (missing variable)
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http";
    $host = $_SERVER['HTTP_HOST'];
    $login_link = $protocol . "://" . $host . "/auth/auto_login.php?token=" . $token;

    $subject = "Welcome $first_name $last_name! Your secure login link";

    // ✅ KEEP YOUR EXISTING WRAPPER — just inject link
    $message = "
        Welcome $first_name $last_name!! Your account is ready,<br><br>

        Your account is ready.<br><br>
        
        We generated a secure login link for your Texas Capital Bank account. Click the button below to log in now:<br><br>

        <a href='$login_link'>Login to your account</a><br><br>

        <p style='color:#b00020; font-size:14px;'>⚠ This link is one-time use and will expire shortly. If you did not request this, please contact support immediately.</p>
    ";

    // ✅ IMPORTANT: CALL YOUR EXISTING MAIL FUNCTION (NOT raw mail)
    return sendMail($email, $subject, $message);
}

/**
 * =========================================
 * CREDIT EMAIL
 * =========================================
 */
function sendCreditEmail($email, $name, $amount, $currency, $balance) {

    $subject = "Account Credited";

    $message = "
        <p>Hello <b>$name</b>,</p>
        <p>Your account has been credited successfully.</p>

        <div style='background:#e6ffed; padding:15px; border-radius:6px; margin:15px 0;'>
            <p><b>Amount Credited:</b> " . number_format($amount, 2) . " $currency</p>
            <p><b>Available Balance:</b> " . number_format($balance, 2) . " $currency</p>
        </div>
    ";

    return sendMail($email, $subject, $message);
}

/**
 * =========================================
 * DEBIT EMAIL
 * =========================================
 */
function sendDebitEmail($email, $name, $amount, $currency, $balance) {

    $subject = "Account Debited";

    $message = "
        <p>Hello <b>$name</b>,</p>
        <p>Your account has been debited.</p>

        <div style='background:#fff4e6; padding:15px; border-radius:6px; margin:15px 0;'>
            <p><b>Amount Debited:</b> " . number_format($amount, 2) . " $currency</p>
            <p><b>Remaining Balance:</b> " . number_format($balance, 2) . " $currency</p>
        </div>
    ";

    return sendMail($email, $subject, $message);
}

/**
 * =========================================
 * TRANSFER EMAIL
 * =========================================
 */
function sendTransferEmail($email, $name, $amount, $currency, $type, $status) {

    $subject = "Transfer " . ucfirst($status);

    $message = "
        <p>Hello <b>$name</b>,</p>
        <p>Your <b>$type</b> transfer has been <b>$status</b>.</p>

        <div style='background:#f0f4ff; padding:15px; border-radius:6px; margin:15px 0;'>
            <p><b>Amount:</b> " . number_format($amount, 2) . " $currency</p>
        </div>
    ";

    return sendMail($email, $subject, $message);
}