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

require_once __DIR__ . '/PHPMailer/src/Exception.php';
require_once __DIR__ . '/PHPMailer/src/PHPMailer.php';
require_once __DIR__ . '/PHPMailer/src/SMTP.php';

function sendStudentConfirmation($toEmail, $toName, $plainPassword, $classSection) {
    $mail = new PHPMailer(true);
    try {
        // Server settings
        $mail->isSMTP();
        $mail->Host       = 'mail.tandddrivingacademy.com';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'info@tandddrivingacademy.com';
        $mail->Password   = 'YOUR_EMAIL_PASSWORD'; // Replace with actual password
        $mail->SMTPSecure = 'ssl';
        $mail->Port       = 465;

        // Recipients
        $mail->setFrom('info@tandddrivingacademy.com', 'T&D Driving Academy');
        $mail->addAddress($toEmail, $toName);

        // Content
        $mail->isHTML(true);
        $mail->Subject = 'Welcome to T&D Driving Academy – Your Portal Access';

        $mail->Body = "<p>Hello <strong>$toName</strong>,</p>
                       <p>Welcome to <strong>T&D Driving Academy</strong>!</p>
                       <p>You have been registered in the <strong>$classSection</strong> class.</p>
                       <p><strong>Login Info:</strong><br>
                       Email: $toEmail<br>
                       Password: $plainPassword</p>
                       <p>You may now log into the student portal to view your drive schedule as it becomes available.</p>
                       <p>– T&D Driving Academy</p>";

        $mail->AltBody = "Hello $toName,\n\nWelcome to T&D Driving Academy.\nClass: $classSection\nEmail: $toEmail\nPassword: $plainPassword\n\n– T&D Driving Academy";

        $mail->send();
        return true;
    } catch (Exception $e) {
        error_log("Email failed: {$mail->ErrorInfo}");
        return false;
    }
}
