<?php
session_start();

if (!isset($_SESSION['pending_verify_type']) || !isset($_SESSION['pending_verify_id'])) {
    header("location:index.php");
    exit;
}

$connPath = ($_SESSION['pending_verify_type'] === 'admin') ? "conn.php" : "conn.php";
include($connPath);
include(__DIR__ . "/email_otp_helper.php");

$error = "";
$type = $_SESSION['pending_verify_type'];
$userId = $_SESSION['pending_verify_id'];

if (isset($_POST['otp'])) {
    $enteredCode = trim($_POST['otp']);

    if (verifyOtp($conn, $type, $userId, $enteredCode)) {
        if ($type === 'admin') {
            $_SESSION['unohs'] = $_SESSION['pending_verify_id'];
            $_SESSION['nirvahaka_hesaru'] = $_SESSION['pending_verify_name'];
        } else {
            $_SESSION['agent_uid'] = $_SESSION['pending_verify_id'];
            $_SESSION['agent_mobile'] = $_SESSION['pending_verify_name'];
        }
        unset($_SESSION['pending_verify_type'], $_SESSION['pending_verify_id'], $_SESSION['pending_verify_name']);
        header("location:dashboard.php");
        exit;
    } else {
        $error = "❌ Code ভুল অথবা মেয়াদ শেষ হয়ে গেছে।";
    }
}

if (isset($_POST['resend'])) {
    $emailCol = $_SESSION['pending_verify_email'] ?? '';
    generateAndSendOtp($conn, $type, $userId, $emailCol);
    $error = "✅ নতুন Code পাঠানো হয়েছে।";
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
<meta charset="UTF-8">
<title>Email Verification</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
    body { background: #0A0A0A; color: #F5EFDC; font-family: sans-serif; display: flex; align-items: center; justify-content: center; min-height: 100vh; margin: 0; }
    .card { background: #171512; border: 1px solid #3A3226; border-radius: 16px; padding: 30px; max-width: 340px; width: 90%; text-align: center; }
    h3 { color: #D4AF37; }
    p { color: #9691B8; font-size: 13px; }
    input { width: 100%; padding: 14px; margin: 16px 0; border-radius: 8px; border: 1px solid #3A3226; background: #0A0A0A; color: #F5EFDC; font-size: 20px; text-align: center; letter-spacing: 6px; box-sizing: border-box; }
    button { width: 100%; padding: 12px; border-radius: 8px; border: none; font-weight: 700; margin-bottom: 10px; cursor: pointer; }
    .btn-verify { background: #D4AF37; color: #1A1400; }
    .btn-resend { background: transparent; color: #9691B8; border: 1px solid #3A3226; }
    .error { color: #E8323C; font-size: 12.5px; margin-bottom: 10px; }
</style>
</head>
<body>
<div class="card">
    <h3>📧 Email Verification</h3>
    <p>তোমার Email-এ পাঠানো ৬-সংখ্যার Code দাও</p>
    <?php if ($error): ?><div class="error"><?= htmlspecialchars($error) ?></div><?php endif; ?>
    <form method="POST">
        <input type="text" name="otp" maxlength="6" placeholder="------" required autofocus>
        <button type="submit" class="btn-verify">Verify করো</button>
    </form>
    <form method="POST">
        <button type="submit" name="resend" value="1" class="btn-resend">আবার Code পাঠাও</button>
    </form>
</div>
</body>
</html>
