<?php

include '../db_connect.php';

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

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

$email = $_POST['email'];
$phone = $_POST['phone'];
$otp   = $_POST['otp'];

$con = dbConnect();

$sql1 = "SELECT * FROM singup_table WHERE phone = '$phone' OR email = '$email'";
$result = mysqli_query($con, $sql1);
$rows = mysqli_num_rows($result);

if ($rows <= 0) {

    $htmlBody = "
    <!DOCTYPE html>
    <html lang='en'>
    <head>
      <meta charset='UTF-8'>
      <meta name='viewport' content='width=device-width, initial-scale=1.0'>
      <title>Verification Code</title>
    </head>
    <body style='margin:0;padding:0;font-family:Arial,sans-serif;background-color:#f2f2f2;'>
      <div style='max-width:600px;margin:20px auto;background-color:#ffffff;border:1px solid #ddd;border-radius:8px;overflow:hidden;box-shadow:0 2px 8px rgba(0,0,0,0.05);'>
        <div style='background:#c60056;padding:20px;text-align:center;'>
          <img src='https://arahlab.xyz/vota/sendotp/logos.png' alt='AppNest BD' style='height:60px;' />
        </div>
        <div style='padding:30px;'>
          <h2 style='color:#333333;'>Your OTP Code</h2>
          <p style='font-size:16px;color:#555;'>Use the following verification code to continue:</p>
          <p style='font-size:28px;color:#c60056;font-weight:bold;text-align:center;margin:20px 0;'>$otp</p>
          <p style='font-size:14px;color:#777;'>This code is valid for <strong>2 minutes</strong>. Please do not share it with anyone.</p>
          <hr style='margin:30px 0;border:0;border-top:1px solid #eee;' />
          <p style='font-size:12px;color:#999;text-align:center;'>This is an automated email. If you did not request this, you can safely ignore it.</p>
        </div>
      </div>
    </body>
    </html>
    ";

    $mail = new PHPMailer(true);

    try {
        $mail->isSMTP();
        $mail->Host       = 'mail.appnestbd.top';
        $mail->SMTPAuth   = true;
        $mail->Username   = 'noreply@votfinity.appnestbd.top';
        $mail->Password   = 'AMHmrM0ri9NhaNFS';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;
        $mail->Port       = 465;

        $mail->CharSet    = 'UTF-8';
        $mail->Encoding   = 'base64';

        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer'       => false,
                'verify_peer_name'  => false,
                'allow_self_signed' => true
            )
        );

        $mail->setFrom('noreply@votfinity.appnestbd.top', 'AppNest BD');
        $mail->addAddress($email);

        $mail->isHTML(true);
        $mail->Subject = 'Your AppNest BD OTP Code';
        $mail->Body    = $htmlBody;

        // Plain text alternative (very important for inbox deliverability)
        $mail->AltBody = "Your OTP code is: $otp\nThis code is valid for 2 minutes. Do not share it with anyone.";

        $mail->send();
        echo 'success';

    } catch (Exception $e) {
        echo 'error: ' . $mail->ErrorInfo;
    }

} else {
    echo 'User already available! Change phone number or email and try again';
}

mysqli_close($con);
?>
