<?php

namespace Korkz\Demirteknoloji\config;

use Korkz\Demirteknoloji\traits\ENVConfig;
use PHPMailer\PHPMailer\Exception;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

class MailConfigure
{
    use ENVConfig;

    private PHPMailer $mail;

    public function __construct($DebugMode = "OFF")
    {
        $this->mail = new PHPMailer(true);
        $ENV = json_decode(json_encode($this->env()));
        $Host = $ENV->MAIL_HOST;
        $Username = $ENV->MAIL_USERNAME;
        $Password = $ENV->MAIL_PASSWORD;
        $Port = $ENV->MAIL_PORT;
        $SMTPAuth = $Port == 465;
        $SMTPSecure = $Port === "465" ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS;
        $Debug = match ($DebugMode) {
            "SERVER" => SMTP::DEBUG_SERVER,
            "CLIENT" => SMTP::DEBUG_CLIENT,
            "CONNECTION" => SMTP::DEBUG_CONNECTION,
            "LOWLEVEL" => SMTP::DEBUG_LOWLEVEL,
            default => SMTP::DEBUG_OFF
        };
        $this->configureSMTP($Host, $Username, $Password, $Port, $SMTPAuth, $SMTPSecure, $Debug);
    }

    private function configureSMTP($Host, $Username, $Password, $Port, $SMTPAuth, $SMTPSecure, $DebugMode): void
    {

        $this->mail->SMTPDebug = $DebugMode;
        $this->mail->isSMTP();
        $this->mail->Host = $Host;
        $this->mail->Username = $Username;
        $this->mail->Password = $Password;
        $this->mail->Port = $Port;
        $this->mail->SMTPAuth = $SMTPAuth;
        $this->mail->SMTPSecure = $SMTPSecure;


    }

    public function mailSender(array $mailers)
    {
        try {
            if (!empty($mailers)) {
                if (isset($mailers["setFrom"]) && isset($mailers["addAddress"]) && isset($mailers["Subject"]) && isset($mailers["Body"])) {
                    $this->mail->setFrom($mailers["setFrom"]["mail"], $mailers["setFrom"]["name"] ?? "");
                    foreach ($mailers["addAddress"] as $mailer) {
                        $this->mail->addAddress($mailer["mail"], $mailer["name"] ?? "");

                    }
                    if (isset($mailers["addReplyTo"])) {

                        $this->mail->addReplyTo($mailers["addReplyTo"][0]["mail"], $mailers["addReplyTo"][0]["name"] ?? "");
                    }
                    if (isset($mailers["addCC"])) {
                        foreach ($mailers["addCC"] as $mailer) {
                            $this->mail->addCC($mailer["mail"], $mailer["name"] ?? "");
                        }
                    }
                    if (isset($mailers["addBCC"])) {
                        foreach ($mailers["addBCC"] as $mailer) {
                            $this->mail->addBCC($mailer["mail"], $mailer["name"] ?? "");
                        }
                    }
                    if (isset($mailers["addAttachment"])) {
                        $files = $mailers["addAttachment"];
                        foreach ($files as $file) {
                            if (file_exists($file["path"])) {
                                $this->mail->addAttachment('/tmp/image.jpg', 'new.jpg');
                            }
                        }
                    }
                    $this->mail->isHTML($mailers["isHTML"]);
                    $this->mail->Subject = $mailers["Subject"];
                    $this->mail->Body = $mailers["Body"];
                    if (isset($mailers["AltBody"])) {
                        $this->mail->AltBody = $mailers["AltBody"];
                    }
                    $this->mail->send();
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception $e) {
            //return false;
            return $this->mail->ErrorInfo;
        }
    }
}
