<?php
include("./db.php");
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

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

header('Content-Type: application/json');
session_start();

$data = json_decode(file_get_contents("php://input"), true);
if (isset($data['ucontact'], $data['upass'])) {
    $ucontact = $data['ucontact'];
    $upass = $data['upass'];

    // Verify user credentials
    $stmt = $con->prepare("SELECT * FROM users WHERE ucontact = ? AND upass = ?");
    $stmt->bind_param("ss", $ucontact, $upass);
    $stmt->execute();
    $result = $stmt->get_result();
    $user = $result->fetch_assoc();
    $stmt->close();

    if ($user) {
        $email = $user['uemail'];
        $uname = $user['uname'];

        // Generate and update new OTP
        $_SESSION['otp'] = $otp = rand(1000, 9999);

        // Send SMS OTP
        $api_key = '45899D6EF8D236';
        $entity = '1501681760000015777';
        $contacts = $ucontact;
        $from = 'DXTECH';
        $sms_text = urlencode(''.$uname.' Password to Recover your Account is '.$otp.' Do not share with anyone. DIGI X');

        //Submit to server
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, "http://manage.sambsms.com/app/smsapi/index.php");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "key=".$api_key."&entity=".$entity."&tempid=1407173554566676394&campaign=0&routeid=7&type=text&contacts=".$contacts."&senderid=".$from."&msg=".$sms_text);

        $smsResponse = curl_exec($ch);
        curl_close($ch);

        $mail = new PHPMailer(true);
        try {
            $mail->isSMTP();
            $mail->Host = 'ayushlpo.com';
            $mail->SMTPAuth = true;
            $mail->Username = 'contact@ayushlpo.com';
            $mail->Password = 'cKIcxib89a0R';
            $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
            $mail->Port = 587;

            $mail->setFrom('contact@ayushlpo.com', 'Ayush Lpo Application');
            $mail->addAddress($email, $uname);

            $mail->isHTML(true);
            $mail->Subject = 'Resend OTP - Ayush Lpo Application';
            $mail->Body = "
                <h3>Dear $uname,</h3>
                <p>Your new OTP is:</p>
                <ul>
                    <li><strong>OTP:</strong> $otp</li>
                </ul>
                <p>Please do not share this OTP with anyone.</p>
                <p>Best regards,<br>Ayush Lpo</p>
            ";

            $mail->send();
            echo json_encode(['message' => 'OTP has been resent to your registerd email and mobile number.']);
        } catch (Exception $e) {
            echo json_encode(['message' => 'Failed to send OTP.']);
        }
    } else {
        echo json_encode(['message' => 'Invalid credentials.']);
    }
} else {
    echo json_encode(['message' => 'Invalid request.']);
}
?>