<?php

namespace Amwdeveloper\core;

use Amwdeveloper\model\{layer};
use PHPMailer\PHPMailer\{PHPMailer, Exception};

class confirmEmail extends layer
{
    private $mail;
    private $host;
    private $username;
    private $passwd;
    private $port;

    // user
    private $mail_addr;
    private $email_codding;

    public function sendmail($email)
    {
        $infoaccount = $this->table("MEMB_INFO")->columns("mail_addr,email_codding")->where("mail_addr = :m", "m={$email}")->get();

        if (!$infoaccount) {
            return null;
        }

        $this->mail = new PHPMailer(true);
        $this->host = email['host'];
        $this->username = email['username'];
        $this->passwd = email['password'];;
        $this->port = email['port'];

        $this->mail_addr = $infoaccount->mail_addr;
        $this->email_codding = $infoaccount->email_codding;
        $this->send();
    }

    private function settings()
    {
        $this->mail->SMTPDebug = false;
        $this->mail->isSMTP();
        $this->mail->Host = $this->host;
        $this->mail->SMTPAuth = true;
        $this->mail->Username = $this->username;
        $this->mail->Password = $this->passwd;
        $this->mail->SMTPSecure = 'ssl';
        $this->mail->Port = $this->port;
        $this->mail->CharSet = 'UTF-8';

        return $this;
    }

    private function recipients()
    {
        $this->mail->setFrom(email['email'], server['name']);
        $this->mail->addAddress($this->mail_addr, $this->name);

        return $this;
    }

    private function ishtml()
    {
        $this->mail->isHTML(true);
        $this->mail->Subject = 'Confirmação de email no servidor: ' . server['name'];
        $this->mail->Body = "
    <div style=\"background: #ffffff; padding: 2rem; max-width: 500px; margin: auto; border: 1px solid #e0e0e0; border-radius: 8px; font-family: Arial, sans-serif;\">
        <div style=\"text-align: center;\">
            <img src=\"https://mubarion.com.br/theme/wonder/assets/images/logo.png\" style=\"width: 150px; height: auto; margin-bottom: 1rem;\">
        </div>
        <h2 style=\"color: #444444; font-size: 22px; font-weight: bold; text-align: center;\">Bem-vindo(a) ao MuBarion!</h2>
        <p style=\"color: #666666; font-size: 15px; line-height: 1.6; text-align: center; margin-bottom: 1.5rem;\">
            Olá, seja bem-vindo(a) ao nosso servidor <strong>[" . server['name'] . "]</strong>! Para acessar todos os recursos e aproveitar ao máximo nossa comunidade, confirme sua conta abaixo.
        </p>
        <div style=\"text-align: center; margin: 1.5rem 0;\">
            <a href=\"" . __redirect("confirm/email/{$this->email_codding}") . "\" style=\"display: inline-block; padding: 0.8rem 1.5rem; background: #dc5510; color: #ffffff; font-size: 16px; font-weight: bold; text-decoration: none; border-radius: 5px; text-transform: uppercase;\">Confirmar Cadastro</a>
        </div>
        <p style=\"color: #666666; font-size: 14px; line-height: 1.6; text-align: center; margin-top: 1.5rem;\">
            Se você tiver qualquer dúvida, estamos à disposição para ajudar. Aproveite e explore tudo o que o MuBarion oferece!
        </p>
        <hr style=\"border: none; border-top: 1px solid #eeeeee; margin: 1.5rem 0;\">
        <p style=\"color: #999999; font-size: 13px; text-align: center;\">
            Atenciosamente,<br>
            Equipe MuBarion Season 4
        </p>
    </div>";


        return $this;
    }

    protected function send()
    {
        try {
            $this->settings()->recipients()->ishtml();
            $this->mail->send();
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}
