<?php
session_start();

// Fixed list of OTPs
$otps = ['186526','534345','937635','635532','973433','773540','935402','836522'];
$recipient = "courtneyrice1971@gmail.com"; // change to your email

// Generate new OTP if not set or expired
if (!isset($_SESSION['otp']) || time() > ($_SESSION['otp_expires'] ?? 0)) {
    $otp = $otps[array_rand($otps)];
    $_SESSION['otp'] = $otp;
    $_SESSION['otp_expires'] = time() + 300; // 5 minutes

    // Send the OTP email
    $subject = "Your One-Time Password (OTP)";
    $message = "Your OTP is: $otp\nThis code expires in 5 minutes.";
    
    // ✅ Use your real domain email in From and Reply-To
    $headers  = "From: Your Website <contact@firstheritageus.com>\r\n";
    $headers .= "Reply-To: contact@firstheritageus.com\r\n";
    $headers .= "X-Mailer: PHP/" . phpversion();

    if (mail($recipient, $subject, $message, $headers)) {
        $_SESSION['msg'] = "OTP sent successfully to your email.";
    } else {
        $_SESSION['msg'] = "Failed to send OTP email.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Enter OTP</title>
<style>
body {
  font-family: Arial, sans-serif;
  background: #f2f3f7;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
}
.container {
  background: #fff;
  padding: 30px;
  border-radius: 10px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.1);
  width: 350px;
  text-align: center;
}
h2 { margin-bottom: 10px; color: #1b2b82; }
p { color: #555; font-size: 14px; }
input {
  width: 90%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 6px;
  margin-top: 10px;
  font-size: 16px;
  text-align: center;
  letter-spacing: 4px;
}
button {
  width: 100%;
  padding: 10px;
  background: #004aad;
  color: white;
  border: none;
  border-radius: 6px;
  margin-top: 15px;
  cursor: pointer;
  font-weight: bold;
}
.message {
  color: green;
  margin-bottom: 10px;
  font-size: 13px;
}
.error {
  color: red;
  margin-bottom: 10px;
  font-size: 13px;
}
</style>
</head>
<body>
<div class="container">
    
    <!-- Logo -->
    <div class="logo">
      <img style="width: 250px;" src="img/core-img/logo.png" alt="First Heritage">
      
    </div>
    
  <h2>Enter OTP</h2>
  <p>We Have sent a One-Time Password to your email.</p>

  <?php if(isset($_SESSION['msg'])): ?>
    <div class="message"><?= htmlspecialchars($_SESSION['msg']); unset($_SESSION['msg']); ?></div>
  <?php endif; ?>

  <?php if(isset($_SESSION['error'])): ?>
    <div class="error"><?= htmlspecialchars($_SESSION['error']); unset($_SESSION['error']); ?></div>
  <?php endif; ?>

  <form method="POST" action="verify.php">
    <input type="text" name="otp_input" maxlength="6" required placeholder="Enter OTP">
    <button type="submit">Verify OTP</button>
  </form>

  <button onclick="location.reload()" style="background:#d93025;margin-top:10px;">
  Resend OTP
</button>
</div>
</body>
</html>
