<?php
// EmailHelper.php

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

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

class EmailHelper {

    private string $smtpHost;
    private string $smtpUsername;
    private string $smtpPassword;
    private int $smtpPort;
    private string $smtpSecure;
    private string $fromEmail;
    private string $fromName;

    public function __construct(
        string $smtpHost = 'smtp.gmail.com',
        string $smtpUsername = 'ibramylez100@gmail.com',
        string $smtpPassword = 'qjgvdhojdefgjbvh', // IMPORTANT: Use environment variables for production!
        int $smtpPort = 587,
        string $smtpSecure = PHPMailer::ENCRYPTION_STARTTLS,
        string $fromEmail = 'ibramylez100@gmail.com',
        string $fromName = 'HR Department - MUSANA COMMUNITY DEVELOPMENT ORGANIZATION LTD'
    ) {
        $this->smtpHost = $smtpHost;
        $this->smtpUsername = $smtpUsername;
        $this->smtpPassword = $smtpPassword;
        $this->smtpPort = $smtpPort;
        $this->smtpSecure = $smtpSecure;
        $this->fromEmail = $fromEmail;
        $this->fromName = $fromName;
    }

    /**
     * Sends an email with an attached PDF.
     *
     * @param string $recipientEmail The email address of the recipient.
     * @param string $recipientName The name of the recipient.
     * @param string $subject The email subject.
     * @param string $body The HTML body of the email.
     * @param string $altBody The plain text alternative body of the email.
     * @param string $pdfContent The binary content of the PDF to attach.
     * @param string $pdfFileName The desired filename for the attached PDF.
     * @throws Exception If there is an error sending the email.
     */
    public function sendEmailWithPdfAttachment(
        string $recipientEmail,
        string $recipientName,
        string $subject,
        string $body,
        string $altBody,
        string $pdfContent,
        string $pdfFileName
    ) {
        if (empty($recipientEmail)) {
            throw new Exception("Missing or empty recipient email address for {$recipientName}. Skipping email.");
        }

        $mail = new PHPMailer(true);

        try {
            // Server settings
            $mail->isSMTP();
            $mail->Host       = $this->smtpHost;
            $mail->SMTPAuth   = true;
            $mail->Username   = $this->smtpUsername;
            $mail->Password   = $this->smtpPassword;
            $mail->SMTPSecure = $this->smtpSecure;
            $mail->Port       = $this->smtpPort;
            $mail->CharSet    = 'UTF-8';

            // IMPORTANT: Add this block to bypass SSL certificate verification for development.
            // DO NOT USE THIS IN PRODUCTION WITHOUT UNDERSTANDING THE SECURITY RISKS.
            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );

            // Recipients
            $mail->setFrom($this->fromEmail, $this->fromName);
            $mail->addAddress($recipientEmail, $recipientName);

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

            // Attach PDF from string
            $mail->addStringAttachment($pdfContent, $pdfFileName, 'base64', 'application/pdf');

            $mail->send();
            error_log("Email successfully sent to {$recipientName} ({$recipientEmail}).");

        } catch (Exception $e) {
            error_log("Email failed for {$recipientName} ({$recipientEmail}): " . $mail->ErrorInfo . " | Exception: " . $e->getMessage());
            throw $e; // Re-throw the exception after logging
        }
    }
}