<?php
require_once 'db.php';
require_once 'functions.php';

require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';
require_once 'PHPMailer/src/Exception.php';

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;



if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    jsonResponse(405, "Invalid request method.");
}

$data = json_decode(file_get_contents('php://input'), true);

if (empty($data['email'])) {
    jsonResponse(400, "Email is required.");
}

$email = decryptData($data['email']);

try {
    $stmt = $pdo->prepare("SELECT id, full_name FROM kd_print_admin WHERE email = ?");
    $stmt->execute([$email]);

    if ($stmt->rowCount() === 0) {
        jsonResponse(404, "Email not found.");
    }

    $admin = $stmt->fetch(PDO::FETCH_ASSOC);

    $otp = random_int(100000, 999999);
    $now = date("Y-m-d H:i:s");

    // Save OTP
   
    $update = $pdo->prepare("UPDATE kd_print_admin SET otp_code = ?, otp_created_at = ? WHERE email = ?");
    $update->execute([$otp, $now, $email]);
  
    // Send OTP by email
    $mail = new PHPMailer(true);
    try {
        // SMTP settings
        $mail->isSMTP();
        $mail->Host = 'email-smtp.ap-south-1.amazonaws.com';       // 🔁 your SMTP host
        $mail->SMTPAuth = true;
        $mail->Username = 'AKIAU6GDVFKR5U5SID7X';        // 🔁 SMTP username
        $mail->Password = 'BJRaGvPSP/Yc5B4SIU2dfp31U2CLjWgI48pV1mg3RZXW';          // 🔁 SMTP password
        $mail->SMTPSecure = 'tls';
        $mail->Port = 587;

        // Email content
        $mail->setFrom('exam@penoft.com', 'Penofts Examination System');
        $mail->addAddress($email, $admin['full_name']);
        $mail->addBCC('abin@penoft.com');
        $mail->isHTML(true);
        $mail->Subject = "Welcome to Penoft's Machine Test Examination";
        $mail->Body = '
    <div style="font-family: Arial, sans-serif; background-color: #f6f6f6; padding: 30px;">
        <div style="max-width: 500px; margin: auto; background: white; border-radius: 10px; padding: 20px; box-shadow: 0 0 10px rgba(0,0,0,0.1);">
            <div style="text-align: center; padding-bottom: 20px;">
                
                <h2 style="color: #2c3e50;">OTP Verification</h2>
            </div>
            <p style="font-size: 16px; color: #333;">
                Dear <strong>' . htmlspecialchars($admin['full_name']) . '</strong>,
            </p>
            <p style="font-size: 16px; color: #333;">
                You requested to reset the password for <strong>Penoft Examination System</strong>.
                Please use the following OTP code to continue:
            </p>
            <div style="text-align: center; margin: 30px 0;">
                <span style="display: inline-block; font-size: 28px; letter-spacing: 10px; color: #007bff; font-weight: bold;">
                    ' . $otp . '
                </span>
            </div>
            <p style="font-size: 14px; color: #666;">
                This OTP is valid for <strong>10 minutes</strong>. Do not share this code with anyone.
            </p>
            <hr style="margin: 30px 0;">
            <p style="font-size: 12px; color: #aaa; text-align: center;">
                Penofts Examination System<br>
                <a href="https://penoft.com" style="color: #aaa;">www.penoft.com</a>
            </p>
        </div>
    </div>
';

    
        $mail->send();
        
        $otp_enc=encryptData($otp);
        
        
        jsonResponse(200, "OTP sent to email successfully.",$otp_enc);

    } catch (Exception $e) {
        jsonResponse(500, "Failed to send OTP email: " . $mail->ErrorInfo);
    }

} catch (Exception $e) {
    jsonResponse(500, "Internal server error: " . $e->getMessage());
}
