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

require_once('mail/src/Exception.php');
require_once('mail/src/PHPMailer.php');
require_once('mail/src/SMTP.php');

$allowed_templates = array("welcome","forgot","reset","set_password","admin_email","verify","loan_approved","loan_added","loan_matured","bvn_error","verification_error","verification_pass","work_verify","notification","loan_denied","support_reply","loan_letter","payout_approval","account_generated","loan_repaid","loan_repaid_pending","loan_denied_amount","notify_loan","approval_loan_schedule","offer_letter","repayment_alert","penalty_charged");
$subjects = array("set_password"=>"Set your Password","verify"=>"Verify your Account","welcome"=>"Welcome to FundsBridge!","forgot"=>"Here's your Password Reset Code!","support"=>"Your message has been sent!","verify"=>"Verify Your Account","loan_added"=>"Your loan request has been submitted!","loan_matured"=>"Your loan is due for repayment!","bvn_error"=>"We could not verify your BVN!","verification_error"=>"We could not verify your ID!","verification_pass"=>"Your account has been verified!","debit_failed"=>"We could not process your payment","work_verify"=>"Verify Work Email","loan_denied"=>"Your loan request has been denied!","loan_approved"=>"Your loan request has been approved!","savings_letter"=>"Your Investment Letter","payout_approval"=>"Approval Required!","account_generated"=>"Your Account has been generated","loan_repaid"=>"Your loan has been repaid","loan_repaid_pending"=>"Loan Repayment","loan_denied_amount"=>"Your loan request has been denied!","notify_loan"=>"Loan Request Notification","approval_loan_schedule"=>"Loan Repayment Approval Requested","offer_letter"=>"Loan Offer Letter","repayment_alert"=>"Your Loan Repayment is Due","penalty_charged"=>"About Your Loan Repayment");

function parseTemplate($text, $data){
    return preg_replace_callback('/{{(\w*)}}/', function($match) use($data){
        return in_array($match[1], array_keys($data)) ? $data[$match[1]] : "";
    }, $text);
}
function get_msg($name, $email, $type,$hash){
    global $allowed_templates;
    $msg = "";
    if(in_array($type, $allowed_templates)){
        if($type == "admin_email"){
            $msg = $hash["body"];
        }else{
            ob_start();
            if(is_array($hash)){
                extract($hash);
            }
            $file = file_get_contents("/home/fundbridge/crons.ballng.com/email_templates/$type.html");
            $msg = parseTemplate($file, $hash);
        }
    }
    return $msg;
}
function send($name, $email, $type,$hash){
    global $subjects;
    $mail = new PHPMailer();
    $mail->IsSMTP();

    $mail->SMTPDebug  = 0;
    $mail->SMTPAuth   = TRUE;
    $mail->SMTPSecure= "ssl";
    $mail->Port       = 465;
    $mail->Host       = "email-smtp.us-east-2.amazonaws.com";
    $mail->Username   = "AKIAZ4CIVPKXNCX4LTNI";
    $mail->Password   = 'BL5+OhSvLxGyM429z12fAGTSvHFO2ltj82YDjGdp9z74';
    $mail->IsHTML(true);
    $mail->SetFrom("help@ballng.com", "BALL NG");
    $multi = array("admin_email");
    if(!str_contains($email, ",")){
        $mail->AddAddress($email, $name);
    }else{
        $mails = explode(",", $email);
        foreach($mails as $m){
            $mail->AddAddress($m, $name);
        }
    }
    if(is_array($hash) && isset($hash["cc"])){
        $ccs = explode(",",$hash["cc"]);
        foreach($ccs as $c){
            $mail->addCC($c);
        }
    }
    $mail->IsHTML(true);
    $mail->Subject = isset($hash["subject"]) ? $hash["subject"] : $subjects[$type];
    $mail->MsgHTML(get_msg($name, $email, $type,$hash));
    if(isset($hash["attachments"]) && sizeof($hash["attachments"]) > 0){
        foreach($hash["attachments"] as $attach){
            $mail->addAttachment($attach);
        }
    }
    if(!$mail->Send()){
        return false; 
    }
    return true;
}
if(isset($_POST["email"])){
    send($_POST["name"], $_POST["email"], $_POST["type"],json_decode($_POST["hash"],true));
    echo "success";
}
?>