<?php
header('Access-Control-Allow-Origin: *');
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');
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
    header('Access-Control-Allow-Origin: *');
    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("HTTP/1.1 200 OK");
    die();
}

include '../../../../config/db.php';
include '../../../../config/mail.php';

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

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $data = json_decode(file_get_contents('php://input'));

    $token = '';
    $email = '';
    $fname = '';
    $lname = '';

    $query = "SELECT * FROM Users WHERE email = '$data->email'";
    if ($results = mysqli_query($con, $query)) {
        if ($results->num_rows == 1) {
            while ($user_data = $results->fetch_assoc()) {

                $token = $user_data['token'];
                $email = $user_data['email'];
                $fname = $user_data['fname'];
                $lname = $user_data['lname'];
            }

            //mail send
            $myAwardSpaceEmail = "technical@ayushnames.com";
            $myAwardSpaceEmailPassword = 'Ayush@2022';
            $myPersonalEmail = $data->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-reset-password/?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' => 'User found successfully.'
                ]);
            } catch (Exception $e) {
                http_response_code(404);
                echo json_encode([
                    'status' => 0,
                    'message' => 'User not found 1.' . $e
                ]);
            }
        } else {
            http_response_code(404);
            echo json_encode([
                'status' => 0,
                'message' => 'User not found 2.',
            ]);
        }
    } else {
        http_response_code(201);
        $statusArr = [
            'status' => 'not logged.'
        ];
        echo json_encode($statusArr);
    }
}
