<?php


namespace App\Services;

use App\Services\ServiceResponse\ServiceResponse;
use App\Services\ServiceResponse\ServiceResponseInterface;
use Exception;
use PHPMailer\PHPMailer\PHPMailer;

/**
 * Class EmailService
 * @package App\Services
 */
class EmailService extends Service
{
    /**
     * @param string $addressee
     * @param string $subject
     * @param string $message
     * @return ServiceResponse
     */
    const fromDefault = 'euler.costa@prolins.com.br';
    const passwd = '@Prolins1';


    public function testMail(string $email): ServiceResponse
    {
        $from_email = self::fromDefault;
        $from_nome = 'Nestor Clínica';
        $log_service = new LogService();
        try {
            $PHPMailer = new PHPMailer(true);
            $this->configureEmail($PHPMailer);
            $PHPMailer->setFrom($from_email, $from_nome);
            $PHPMailer->addAddress("sharkboyevil0009k9999@sharklasers.com");
            $PHPMailer->Subject = "e-mail nestor";
            $PHPMailer->Body = "funcionando";
            $sucess_sending_email = $PHPMailer->send();
        } catch (Exception $exception) {
            $log_service->log_error($exception->getCode(), $exception->getMessage(), []);
            $sucess_sending_email = false;
        }
        $service_response = ServiceResponse::builder()
            ->setStatus($sucess_sending_email ? ServiceResponseInterface::SUCESS : ServiceResponseInterface::SERVICE_INTERNAL_ERROR)
            ->setErrors($sucess_sending_email ? [] : ['email-service' => 'Erro no envio do email solicitado.'])
            ->build();
        return $service_response;
    }
    
    public function send_email(string $addressee, string $subject, string $message): ServiceResponse
    {
        $from_email = self::fromDefault;
        $from_nome = 'Pettree';
        $log_service = new LogService();
        try {
            $PHPMailer = new PHPMailer(true);
            $this->configureEmail($PHPMailer);
            $PHPMailer->setFrom($from_email, $from_nome);
            $PHPMailer->addAddress($addressee);
            $PHPMailer->Subject = utf8_decode($subject);
            $PHPMailer->Body = $message;
            $sucess_sending_email = $PHPMailer->send();
        } catch (Exception $exception) {
            $log_service->log_error($exception->getCode(), $exception->getMessage(), []);
            $sucess_sending_email = false;
        }
        
        $service_response = ServiceResponse::builder()
            ->setStatus($sucess_sending_email ? ServiceResponseInterface::SUCESS : ServiceResponseInterface::SERVICE_INTERNAL_ERROR)
            ->setErrors($sucess_sending_email ? [] : ['email-service' => 'Erro no envio do email solicitado.'])
            ->build();
        return $service_response;
    }

    private function configureEmail(PHPMailer $PHPMailer): void
    {
//        $PHPMailer->isSMTP();
//        $PHPMailer->isHTML(true);
//        $PHPMailer->SMTPAuth = false;
////        $PHPMailer->Port = 587;
//        $PHPMailer->Port = 465;
////        $PHPMailer->SMTPSecure = true;
//        //$PHPMailer->SMTPSecure = 'tls';
////        $PHPMailer->SMTPAutoTLS = true;
//        $PHPMailer->Host = 'email-ssl.com.br';
//        $PHPMailer->Username = self::fromDefault;
//        $PHPMailer->Password = self::passwd;
//        $PHPMailer->CharSet = 'utf-8';

        $PHPMailer = new PHPMailer();
        $PHPMailer->isSMTP();
        $PHPMailer->Host = 'email-ssl.com.br';
        $PHPMailer->SMTPAuth = false;
        $PHPMailer->Port = 465;
        $PHPMailer->isHTML( true );
        $PHPMailer->CharSet = 'utf-8';
    }
}



























//namespace App\Services;
//
//use App\Services\ServiceResponse\ServiceResponse;
//use App\Services\ServiceResponse\ServiceResponseInterface;
//use Config\Services;
//
//class EmailService extends Service
//{
//    public function send_email(string $addressee, string $subject, string $message) : ServiceResponse
//    {
//        $email_service = Services::email();
//        $email_service->setTo($addressee);
//        $email_service->setFrom($email_service->$SMTPUser, 'Nestor Clínica');
//        $email_service->setSubject($subject);
//        $email_service->setMessage($message);
//        $sucess_sending_email = !!$email_service->send();
//        $service_response = ServiceResponse::builder()
//            ->setStatus($sucess_sending_email ? ServiceResponseInterface::SUCESS : ServiceResponseInterface::SERVICE_INTERNAL_ERROR)
//            ->setErrors($sucess_sending_email ? [] : ['email-service' => 'Erro no envio do email solicitado.'])
//            ->build();
//        if ($service_response->getStatus() === ServiceResponseInterface::SERVICE_INTERNAL_ERROR) {
//            $log_service = new LogService();
//            $log_service->log_error($service_response->getStatus(), 'Ocorre um erro durante o envio do email.', $service_response->getErrors());
//        }
//        return $service_response;
//    }
//}