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

require_once __DIR__ . '/../vendor/autoload.php'; // Adjust path as needed

class Mailer {
    private $mail;

    public function __construct() {
        $this->mail = new PHPMailer(true);
        $this->setup();
    }

    private function setup() {
        $this->mail->isSMTP();
        $this->mail->Host       = 'mail.schoolmata.com.ng';
        $this->mail->SMTPAuth   = true;
        $this->mail->Username   = 'support@schoolmata.com.ng';
        $this->mail->Password   = 'eCPv8VQ]~z2O';
        $this->mail->SMTPSecure = 'tls';
        $this->mail->Port       = 587;
        $this->mail->setFrom('support@schoolmata.com.ng', 'SchoolMata App');
        $this->mail->isHTML(true);
    }

    public function send($to, $subject, $message) {
        try {
            $this->mail->clearAddresses();
            $this->mail->addAddress($to);
            $this->mail->Subject = $subject;
            $this->mail->Body    = $message;
            $this->mail->AltBody = strip_tags($message);

            $this->mail->send();
            return ['status' => 'success', 'message' => 'Mail sent successfully'];
        } catch (Exception $e) {
            return ['status' => 'error', 'message' => $this->mail->ErrorInfo];
        }
    }
}
