<?php
/**
 * PHPMailer Configuration
 */

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Include PHPMailer (Composer autoload if present, otherwise local PHPMailer source)
$composerAutoload = __DIR__ . '/../vendor/autoload.php';
if (file_exists($composerAutoload)) {
    require_once $composerAutoload;
} else {
    require_once __DIR__ . '/../PHPMailer/src/Exception.php';
    require_once __DIR__ . '/../PHPMailer/src/PHPMailer.php';
    require_once __DIR__ . '/../PHPMailer/src/SMTP.php';
}

/**
 * Send email using PHPMailer
 */
function sendEmail($recipientEmail, $recipientName, $subject, $htmlBody) {
    try {
        $mail = new PHPMailer(true);

        // Server settings
        $mail->isSMTP();
        $mail->Host = getenv('MAIL_HOST') ?: 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = getenv('MAIL_USERNAME') ?: 'mohamedhalith117@gmail.com';
        $mail->Password = getenv('MAIL_PASSWORD') ?: 'arctfbzmhhddkfqr';
        $mail->SMTPSecure = getenv('MAIL_ENCRYPTION') ?: PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = (int)(getenv('MAIL_PORT') ?: 587);

        // Recipients
        $mail->setFrom(getenv('MAIL_FROM') ?: 'mohamedhalith117@gmail.com', getenv('MAIL_FROM_NAME') ?: 'Zemaraim Hotel');
        $mail->addAddress($recipientEmail, $recipientName);

        // Content
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body = $htmlBody;
        $mail->AltBody = strip_tags($htmlBody);

        $mail->send();
        return true;
    } catch (Exception $e) {
        error_log('PHPMailer Error: ' . $e->getMessage());
        return false;
    }
}

/**
 * Send OTP email
 */
function sendOTPEmail($email, $firstName, $otp, $purpose = 'verification') {
    $subject = 'Your Zemaraim Verification Code';
    $requestType = 'registration';
    
    if ($purpose === 'password_reset') {
        $subject = 'Zemaraim Password Reset Code';
        $requestType = 'password reset';
    }

    $htmlBody = <<<HTML
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <style>
            body { font-family: Arial, sans-serif; color: #333; }
            .container { max-width: 600px; margin: 0 auto; padding: 20px; }
            .header { background: linear-gradient(135deg, #4CAF50 0%, #2E7D32 100%); color: white; padding: 20px; text-align: center; border-radius: 5px; }
            .content { background: #f5f5f5; padding: 20px; margin-top: 10px; border-radius: 5px; }
            .otp-box { background: white; padding: 20px; text-align: center; margin: 20px 0; border: 2px solid #4CAF50; border-radius: 5px; }
            .otp-code { font-size: 32px; font-weight: bold; color: #4CAF50; letter-spacing: 5px; }
            .footer { text-align: center; margin-top: 20px; font-size: 12px; color: #666; }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>Zemaraim Hotel Booking</h1>
            </div>
            <div class="content">
                <p>Hello {$firstName},</p>
                <p>We received a request to verify your email address. Use the code below to complete your {$requestType}:</p>
                <div class="otp-box">
                    <div class="otp-code">{$otp}</div>
                    <p style="margin-top: 10px; color: #666;">This code will expire in 10 minutes</p>
                </div>
                <p><strong>Important:</strong> Never share this code with anyone. Our team will never ask for this code.</p>
                <p>If you didn't request this, please ignore this email.</p>
            </div>
            <div class="footer">
                <p>&copy; 2026 Zemaraim Hotel. All rights reserved.</p>
                <p>This is an automated email. Please do not reply to this email address.</p>
            </div>
        </div>
    </body>
    </html>
    HTML;

    return sendEmail($email, $firstName, $subject, $htmlBody);
}

?>
