<?php
/**
 * Mail Channel Class
 *
 * @version     1.0.$Revision:$
 * @version     SVN: $Id:$
 * @package     bplan-base/globals
 * @subpackage  Channels
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 * @copyright   Copyright (C) 2025, 2026 bplan-solutions GmbH & Co. KG <https://www.bplan-solutions.de/>
 * /Δ\
 */

namespace BplanBase\Globals\Channels;


use BplanBase\Globals\Mail\PHPMailer;
use Illuminate\Notifications\Notification;


/**
 * Mail Channel Class
 *
 * @version     1.0.0 / 2026-04-22
 * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
 */
class PHPMailerChannel
{


/* +++ TRAITS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ CLASS CONSTANTS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++ */


/* +++ OBJECT MEMBERS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */

    /**
     * @var     PHPMailer|null
     */
    protected PHPMailer|null $_Mailer = null;


/* +++ CLASS MEMBERS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     * @var     string
     */
    private static string $_logPrefix = 'Channels/PHPMailerChannel';


/* +++ OBJECT METHODS +++ +++++++++++++++++++++++++++++++++++++++++++++++++++ */


    /**
     *
     * @version     1.1.0 / 2026-04-22
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function __construct()
    {
        if (config('mail.default') !== 'log') {
            $this->_Mailer = new PHPMailer(true);
        }

    } // __construct()


    /**
     * Send the given notification.
     *
     * @param       mixed $Notifiable
     *
     * @param       Notification $Notification
     *
     * @return      bool
     *
     * @version     1.0.0 / 2026-04-22
     * @author      Wassilios Meletiadis <wassilios.meletiadis@bplan-solutions.de>
     */
    public function send(mixed $Notifiable, Notification $Notification): bool
    {
        $Message = $Notification->toPHPMailer($Notifiable);

        if ($this->_Mailer === null) {
            $recipients = implode(', ', array_column($Message->getRecipients(), 'email'));

            \AppLog::info('[mail.default=log] - Email is not being sent. Subject: ['.$Message->getSubject().'], Recipients: ['.$recipients.']', prefix: self::$_logPrefix);

            return true;
        }
        if (($from = $Message->getFrom()) !== null) {
            $this->_Mailer->setFrom($from['email'], $from['name']);
        }
        foreach ($Message->getRecipients() as $recipient) {
            $this->_Mailer->addAddress($recipient['email'], $recipient['name']);
        }
        foreach ($Message->getCcRecipients() as $recipient) {
            $this->_Mailer->addCC($recipient['email'], $recipient['name']);
        }
        foreach ($Message->getBccRecipients() as $recipient) {
            $this->_Mailer->addBCC($recipient['email'], $recipient['name']);
        }
        foreach ($Message->getReplyTo() as $recipient) {
            $this->_Mailer->addReplyTo($recipient['email'], $recipient['name']);
        }
        foreach ($Message->getAttachments() as $attachment) {
            $this->_Mailer->addAttachment($attachment['file'], $attachment['name']);
        }
        $this->_Mailer->isHTML(true);

        $this->_Mailer->AltBody = $Message->getAltBody();
        $this->_Mailer->Body = $Message->getBody();
        $this->_Mailer->Ical = $Message->getIcal();
        $this->_Mailer->Subject = $Message->getSubject();

        return $this->_Mailer->send();

    } // send()


/* +++ CLASS METHODS +++ ++++++++++++++++++++++++++++++++++++++++++++++++++++ */


} // class PHPMailerChannel {}
