<?php
// Include these at the very top of the file

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

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

function sendEmailOTP($recipientEmail, $otp) {
    $mail = new PHPMailer(true);

    try {
        // --- SMTP Server Settings ---
        $mail->isSMTP();
        $mail->Host       = 'smtp.gmail.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'mushtaqueahmed178@gmail.com'; // Your Gmail address
        $mail->Password   = 'irft pnqt hqiv ggtx'; // Your 16-character App Password
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port       = 587;

        // --- Sender & Recipient ---
        $mail->setFrom('mushtaqueahmed178@gmail.com', 'School Admin Portal');
        $mail->addAddress($recipientEmail);

        // --- Email Content ---
        $mail->isHTML(true);
        $mail->Subject = "Your Login Verification Code: $otp";
        
        // Beautiful HTML Body
        $mail->Body = "
            <div style='font-family: Arial, sans-serif; border: 1px solid #ddd; padding: 20px; border-radius: 10px; max-width: 400px;'>
                <h2 style='color: #4e73df;'>Security Verification</h2>
                <p>A login attempt was made for your Admin account. Use the code below to verify your identity:</p>
                <div style='background: #f8f9fc; padding: 15px; text-align: center; font-size: 32px; font-weight: bold; letter-spacing: 5px; color: #4e73df; border-radius: 5px;'>
                    $otp
                </div>
                <p style='margin-top: 20px; font-size: 12px; color: #888;'>This code will expire in 10 minutes. If you did not request this, please secure your account immediately.</p>
            </div>
        ";
        
        $mail->AltBody = "Your verification code is: $otp";

        $mail->send();
        return true; // Success
    } catch (Exception $e) {
        // Return the error message so your login_action.php can catch it
        return "Mailer Error: " . $mail->ErrorInfo;
    }
}
?>