<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

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

session_start();

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

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['email'])) {
    $email = $_POST['email'];
    
    // Generate 6-digit code
    $code = rand(100000, 999999);
    $_SESSION['verification_code'] = $code;
    $_SESSION['email_to_verify'] = $email;

    $mail = new PHPMailer(true);

    try {
        // Server settings
        $mail->isSMTP();
        $mail->Host       = 'mail.gobolchain.info';     // Replace with your mail server
        $mail->SMTPAuth   = true;
        $mail->Username   = 'team@gobolchain.info'; // Replace with your email
        $mail->Password   = '22J4FV5AXSTPMC7R';     // Replace with your password
        $mail->SMTPSecure = 'ssl';                     // Or 'ssl' depending on your host
        $mail->Port       = 465;                       // Usually 465 for SSL, 587 for TLS

        // Recipients
        $mail->setFrom('team@gobolchain.info', 'Gobolchain');
        $mail->addAddress($email);

        // Content
        $mail->isHTML(true);
        $mail->Subject = 'Your Gobolchain Verification Code';
        $mail->Body    = "Your verification code is: <strong>$code</strong>";

        $mail->send();
        echo "Verification code sent successfully to $email";
    } catch (Exception $e) {
        echo "Failed to send code. Mailer Error: {$mail->ErrorInfo}";
    }
} else {
    echo "Invalid request.";
}