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

require_once __DIR__ . '/../vendor/autoload.php';

/**
 * Send email using PHPMailer with your working SMTP settings
 * @param string $recipientEmail - Email address of the recipient
 * @param string $subject - Email subject
 * @param string $message - HTML message body
 * @return bool - True if sent successfully, false otherwise
 */
function sendEmail($recipientEmail, $subject, $message) {
    $mail = new PHPMailer(true);
    
    try {
        // Server settings - Using your WORKING configuration
        $mail->isSMTP();
        $mail->Host       = 's4.whitelabelclouds.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'info@clearpathtalentgroup.com';
        $mail->Password   = 'Clearpath123';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port       = 465;
        
        // Disable debug output for production
        $mail->SMTPDebug = 0;
        
        // Recipients
        $mail->setFrom('info@clearpathtalentgroup.com', 'Oakshire Finance');
        $mail->addAddress($recipientEmail);
        
        // Content
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $message;
        $mail->AltBody = strip_tags($message);
        
        $mail->send();
        error_log("Email sent successfully to $recipientEmail - Subject: $subject");
        return true;
        
    } catch (Exception $e) {
        error_log("Email Error to $recipientEmail: " . $mail->ErrorInfo);
        error_log("Exception: " . $e->getMessage());
        return false;
    }
}

/**
 * Send verification code email for withdrawal (3-step verification)
 * @param string $email - User's email
 * @param string $name - User's full name
 * @param int $step - Which step (1, 2, or 3)
 * @param string $code - 6-digit verification code
 * @param float $amount - Withdrawal amount
 * @param int $pending_id - Transaction reference ID
 * @return bool
 */
function sendVerificationCode($email, $name, $step, $code, $amount, $pending_id) {
    $step_word = $step == 1 ? 'first' : ($step == 2 ? 'second' : 'third');
    $step_number = $step;
    
    $subject = "🔐 Verification Code #$step - Oakshire Finance Withdrawal";
    
    $message = "
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset='UTF-8'>
        <meta name='viewport' content='width=device-width, initial-scale=1.0'>
        <title>Verification Code</title>
        <style>
            body {
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
                line-height: 1.6;
                color: #333;
                background-color: #f4f4f4;
                margin: 0;
                padding: 0;
            }
            .container {
                max-width: 550px;
                margin: 0 auto;
                padding: 20px;
                background-color: #ffffff;
            }
            .header {
                background: linear-gradient(135deg, #0a2540 0%, #1a4d8c 100%);
                padding: 25px 20px;
                text-align: center;
                border-radius: 12px 12px 0 0;
            }
            .header h1 {
                color: white;
                margin: 0;
                font-size: 24px;
                font-weight: 700;
            }
            .header p {
                color: rgba(255,255,255,0.8);
                margin: 5px 0 0;
                font-size: 12px;
            }
            .content {
                background: white;
                padding: 30px;
                border: 1px solid #e9ecef;
                border-top: none;
                border-radius: 0 0 12px 12px;
            }
            .greeting {
                font-size: 16px;
                margin-bottom: 20px;
                color: #0a2540;
            }
            .amount-box {
                background: #f8f9fc;
                padding: 12px;
                border-radius: 8px;
                text-align: center;
                margin: 15px 0;
            }
            .amount {
                font-size: 22px;
                font-weight: 700;
                color: #0a2540;
            }
            .code-box {
                background: #0a2540;
                color: white;
                font-size: 32px;
                font-weight: bold;
                letter-spacing: 6px;
                padding: 18px;
                text-align: center;
                border-radius: 12px;
                margin: 25px 0;
                font-family: monospace;
            }
            .step-box {
                background: #e8f4f8;
                padding: 12px;
                border-radius: 8px;
                margin: 15px 0;
                text-align: center;
                border-left: 4px solid #1a4d8c;
            }
            .warning {
                background: #fff3cd;
                border-left: 4px solid #ffc107;
                padding: 12px;
                margin: 15px 0;
                border-radius: 8px;
                font-size: 13px;
            }
            .footer {
                text-align: center;
                padding: 20px;
                font-size: 11px;
                color: #6c757d;
                border-top: 1px solid #e9ecef;
                margin-top: 20px;
            }
            .status-steps {
                display: flex;
                justify-content: space-between;
                margin-bottom: 25px;
            }
            .step {
                text-align: center;
                flex: 1;
            }
            .step .circle {
                width: 35px;
                height: 35px;
                background: #e9ecef;
                border-radius: 50%;
                display: flex;
                align-items: center;
                justify-content: center;
                margin: 0 auto 6px;
                font-weight: bold;
                font-size: 14px;
                color: #6c757d;
            }
            .step.active .circle {
                background: #0a2540;
                color: white;
            }
            .step.completed .circle {
                background: #2e7d32;
                color: white;
            }
            .step .label {
                font-size: 10px;
                color: #6c757d;
            }
            .step.active .label {
                color: #0a2540;
                font-weight: 600;
            }
            hr {
                margin: 20px 0;
                border: none;
                border-top: 1px solid #e9ecef;
            }
            .button {
                display: inline-block;
                padding: 10px 20px;
                background: #0a2540;
                color: white;
                text-decoration: none;
                border-radius: 6px;
                margin-top: 10px;
            }
        </style>
    </head>
    <body>
        <div class='container'>
            <div class='header'>
                <h1>Oakshire Finance</h1>
                <p>Secure Banking • Trusted Since 2019</p>
            </div>
            <div class='content'>
                <div class='status-steps'>
                    <div class='step " . ($step_number >= 1 ? ($step_number > 1 ? 'completed' : 'active') : '') . "'>
                        <div class='circle'>" . ($step_number > 1 ? '✓' : '1') . "</div>
                        <div class='label'>Code 1</div>
                    </div>
                    <div class='step " . ($step_number >= 2 ? ($step_number > 2 ? 'completed' : 'active') : '') . "'>
                        <div class='circle'>" . ($step_number > 2 ? '✓' : '2') . "</div>
                        <div class='label'>Code 2</div>
                    </div>
                    <div class='step " . ($step_number >= 3 ? 'active' : '') . "'>
                        <div class='circle'>3</div>
                        <div class='label'>Code 3</div>
                    </div>
                </div>
                
                <div class='greeting'>
                    Dear <strong>" . htmlspecialchars($name) . "</strong>,
                </div>
                
                <p>You have requested a withdrawal of:</p>
                <div class='amount-box'>
                    <span class='amount'>$" . number_format($amount, 2) . "</span>
                </div>
                
                <div class='step-box'>
                    <strong>Step $step_number of 3</strong><br>
                    Please use the code below to verify this step:
                </div>
                
                <div class='code-box'>$code</div>
                
                <div class='warning'>
                    <strong>⚠️ Important</strong><br>
                    • Code expires in <strong>24 hours</strong><br>
                    • Never share this code<br>
                    • You need to complete all 3 steps
                </div>
                
                <p><strong>Next Steps:</strong></p>
                <ol style='margin-left: 20px; font-size: 13px;'>
                    <li>Login to your Oakshire Finance account</li>
                    <li>Go to <strong>Pending Withdrawals</strong></li>
                    <li>Click on this withdrawal request</li>
                    <li>Enter the code: <strong>$code</strong></li>
                    <li>Wait for the next code from admin</li>
                </ol>
                
                <hr>
                
                <p style='font-size: 12px; color: #6c757d;'>
                    <strong>Reference:</strong> #$pending_id<br>
                    <strong>Date:</strong> " . date('F j, Y g:i A') . "
                </p>
            </div>
            <div class='footer'>
                <p>Oakshire Finance - Where Money Meets Trust</p>
                <p>123 Financial District, Makati City, Philippines</p>
                <p>This is an automated message. Please do not reply.</p>
            </div>
        </div>
    </body>
    </html>
    ";
    
    return sendEmail($email, $subject, $message);
}

/**
 * Send transaction notification email
 * @param string $email - User's email
 * @param string $name - User's name
 * @param string $type - Transaction type (deposit, withdrawal, transfer)
 * @param float $amount - Transaction amount
 * @param string $status - Transaction status
 * @param string $reference - Transaction reference
 * @return bool
 */
function sendTransactionNotification($email, $name, $type, $amount, $status, $reference) {
    $subject = "📝 Transaction $status - Oakshire Finance";
    
    $message = "
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset='UTF-8'>
        <title>Transaction Notification</title>
        <style>
            body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
            .container { max-width: 550px; margin: 0 auto; padding: 20px; background: white; }
            .header { background: #0a2540; padding: 20px; text-align: center; border-radius: 10px 10px 0 0; }
            .header h1 { color: white; margin: 0; }
            .content { padding: 30px; border: 1px solid #e9ecef; border-top: none; border-radius: 0 0 10px 10px; }
            .status-approved { color: #2e7d32; font-weight: bold; }
            .status-pending { color: #f39c12; font-weight: bold; }
            .status-rejected { color: #dc3545; font-weight: bold; }
            .footer { text-align: center; padding: 20px; font-size: 11px; color: #6c757d; }
        </style>
    </head>
    <body>
        <div class='container'>
            <div class='header'>
                <h1>Oakshire Finance</h1>
            </div>
            <div class='content'>
                <h2>Transaction $status</h2>
                <p>Dear <strong>" . htmlspecialchars($name) . "</strong>,</p>
                <p>Your $type of <strong>$" . number_format($amount, 2) . "</strong> has been <strong class='status-" . strtolower($status) . "'>$status</strong>.</p>
                <p><strong>Reference:</strong> $reference</p>
                <p><strong>Date:</strong> " . date('F j, Y g:i A') . "</p>
                <p>Thank you for banking with Oakshire Finance!</p>
            </div>
            <div class='footer'>
                <p>Oakshire Finance - Where Money Meets Trust</p>
            </div>
        </div>
    </body>
    </html>
    ";
    
    return sendEmail($email, $subject, $message);
}

/**
 * Send login alert email
 * @param string $email - User's email
 * @param string $name - User's name
 * @param string $ip - IP address
 * @param string $device - Device info
 * @return bool
 */
function sendLoginAlert($email, $name, $ip, $device) {
    $subject = "🔐 New Login to Your Oakshire Finance Account";
    
    $message = "
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset='UTF-8'>
        <title>Login Alert</title>
        <style>
            body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; }
            .container { max-width: 550px; margin: 0 auto; padding: 20px; background: white; }
            .header { background: #0a2540; padding: 20px; text-align: center; border-radius: 10px 10px 0 0; }
            .header h1 { color: white; margin: 0; }
            .content { padding: 30px; border: 1px solid #e9ecef; border-top: none; border-radius: 0 0 10px 10px; }
            .warning { background: #fff3cd; border-left: 4px solid #ffc107; padding: 15px; margin: 20px 0; }
            .footer { text-align: center; padding: 20px; font-size: 11px; color: #6c757d; }
        </style>
    </head>
    <body>
        <div class='container'>
            <div class='header'>
                <h1>Oakshire Finance</h1>
            </div>
            <div class='content'>
                <h2>New Login Detected</h2>
                <p>Dear <strong>" . htmlspecialchars($name) . "</strong>,</p>
                <p>A new login to your account was detected:</p>
                <p><strong>IP Address:</strong> $ip</p>
                <p><strong>Device:</strong> $device</p>
                <p><strong>Time:</strong> " . date('F j, Y g:i A') . "</p>
                <div class='warning'>
                    <strong>⚠️ If this wasn't you</strong><br>
                    Please contact our support team immediately.
                </div>
                <p>If this was you, you can safely ignore this message.</p>
            </div>
            <div class='footer'>
                <p>Oakshire Finance - Where Money Meets Trust</p>
            </div>
        </div>
    </body>
    </html>
    ";
    
    return sendEmail($email, $subject, $message);
}
?>