<?php
// Email helper functions extracted from functions.php
// Loads PHPMailer if present and exposes two functions:
// - sendAccountCreationEmail
// - sendSysadAccountCreationEmail

// Load PHPMailer sources (bundled under sysad/admin/PHPMailer)
$phpMailerDir = __DIR__ . '/PHPMailer/src/';
if (file_exists($phpMailerDir . 'PHPMailer.php')) {
    require_once $phpMailerDir . 'PHPMailer.php';
    require_once $phpMailerDir . 'SMTP.php';
    require_once $phpMailerDir . 'Exception.php';
}


// Send password reset email with token link
if (!function_exists('sendPasswordResetEmail')) {
    function sendPasswordResetEmail($toEmail, $nameOrUsername, $token, $isSysad = false) {
        if (empty($toEmail) || !filter_var($toEmail, FILTER_VALIDATE_EMAIL) || empty($token)) {
            return false;
        }

    // Base URL for password reset links - point to the public reset endpoint
    $baseUrl = 'https://pmed.lagunappdco.online';
    $resetUrl = $baseUrl . '/forgot_password_reset.php?token=' . urlencode($token);

        $subject = 'Password Reset Request';
        $htmlBody = '<p>Hello ' . htmlspecialchars($nameOrUsername) . ',</p>' .
                    '<p>We received a request to reset your password. Click the link below (valid for 1 hour):</p>' .
                    '<p><a href="' . htmlspecialchars($resetUrl) . '">' . htmlspecialchars($resetUrl) . '</a></p>' .
                    '<p>If you didn’t request this, you can ignore this email.</p>';

        $plainBody = "Hello $nameOrUsername,\n\n" .
                     "We received a request to reset your password. Use the link below (valid for 1 hour):\n" .
                     "$resetUrl\n\n" .
                     "If you didn’t request this, you can ignore this email.\n";

        $fromEmail = defined('EMAIL_FROM') ? EMAIL_FROM : 'office@lagunappdco.online';
        $fromName = defined('EMAIL_FROM_NAME') ? EMAIL_FROM_NAME : 'PPDCO Accounts';

    if (class_exists('\PHPMailer\PHPMailer\PHPMailer')) {
            try {
                $mail = new \PHPMailer\PHPMailer\PHPMailer(true);
                if (defined('SMTP_HOST') && SMTP_HOST) {
                    $mail->isSMTP();
                    $mail->Host = SMTP_HOST;
                    $mail->SMTPAuth = (defined('SMTP_USER') && defined('SMTP_PASS'));
                    if (defined('SMTP_USER')) $mail->Username = SMTP_USER;
                    if (defined('SMTP_PASS')) $mail->Password = SMTP_PASS;
                    if (defined('SMTP_SECURE')) $mail->SMTPSecure = SMTP_SECURE;
                    if (defined('SMTP_PORT')) $mail->Port = SMTP_PORT;
                } else {
                    $mail->isMail();
                }
                $mail->setFrom($fromEmail, $fromName);
                $mail->addAddress($toEmail);
                $mail->Subject = $subject;
                $mail->isHTML(true);
                $mail->Body = $htmlBody;
                $mail->AltBody = $plainBody;
                $mail->send();
                return true;
            } catch (\PHPMailer\PHPMailer\Exception $e) {
                // Fall through to mail()
            }
        }

        $headers = 'From: ' . $fromName . ' <' . $fromEmail . '>' . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=utf-8' . "\r\n";
        return mail($toEmail, $subject, $plainBody, $headers);
    }
}
?>