<?php

//include headers

header("Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization");
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');

require './libs/Exception.php';
require './libs/PHPMailer.php';
require './libs/SMTP.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$responseArray = [];

$data = file_get_contents("php://input");

if (isset($data) && !empty($data)) {

  $request = json_decode($data);

  if (trim($request->email) !== '' || trim($request->fname) !== '' || trim($request->lname) !== '') {


    $myAwardSpaceEmail = "technical@ayushnames.com";
    $myAwardSpaceEmailPassword = 'Ayush@2022';
    $myPersonalEmail = $request->email;


    $name =  $request->name;
    $email = $request->email;
    $reply_email = 'info@ayushnames.com';

    $mail = new PHPMailer(true);
    $mail->SMTPDebug = 0;
    $mail->Host = 'ftp.ayushnames.com';
    $mail->SMTPAuth = true;
    $mail->Username = $myAwardSpaceEmail;
    $mail->Password = $myAwardSpaceEmailPassword;
    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;

    $mail->setFrom($myAwardSpaceEmail, 'Ayush Names - Verify My Account');
    $mail->addAddress($myPersonalEmail);
    $mail->addReplyTo($reply_email, 'Ayush Names');
    $mail->isHTML(true);
    $mail->Subject = 'Ayush Names - Verify My Account';

    $txt = '<div style="margin-left: auto;margin-right: auto;width: 55%;">
            <div style="border: 0.5px solid #d7d7d7;border-radius: 10px;padding: 20px;width: fit-content;font-family: system-ui;">
            <div style="width: 50%;">
            <img style="height: auto; width: 100%;" src="https://ayushnames.com/wp-content/uploads/2022/03/Artboard-2@4x-3-373x147.png">
            </div>
            <p>Dear ' . $fname . ' ' . $lname . ', </p>
            <p> Please click on this link to verify your account on Ayush names.</p>
            <a style="width: 200px;display: block;background: orange;color: #ffffff;padding: 10px;margin: auto;text-align: center;text-decoration: none;border-radius: 10px;" href="https://ayushnames.com/verify-user/?token=' . $token . '&email=' . $email . '">Verify Me</a>
            <p> Thank you,</p>
            <p>Ayush team</p>
            </div>
            </div>';

    $mail->Body = $txt;

    try {

      $mail->send();

      http_response_code(200);
      echo json_encode([
        'status' => 1,
        'message' => 'Mail was sent successfully!',
        'data' => $request

      ]);
    } catch (Exception $e) {

      http_response_code(400);
      echo json_encode([
        'status' => 0,
        'message' => "Your message could not be sent! PHPMailer Error: {$mail->ErrorInfo}"
      ]);
    }
  } else {

    $responseArray = [
      'message' => 'Missing required data.',
      'status' => 0,
      'details' => json_decode($data)
    ];

    echo json_encode($responseArray);
  }
} else {
  $responseArray = [
    'message' => 'Cannot send empty values.',
    'status' => -0
  ];

  echo json_encode($responseArray);
}
