<?php
session_start();
include __DIR__ . '/../database/search_log_config.php';

$message = "";
$error = "";

if($_SERVER["REQUEST_METHOD"] === "POST"){

    $email = trim($_POST['email']);

    $stmt = $log_conn->prepare("SELECT id FROM admin_users WHERE admin_email=?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    if($result->num_rows === 1){

        $token = bin2hex(random_bytes(32));
        $expiry = date("Y-m-d H:i:s", strtotime("+1 hour"));

        $update = $log_conn->prepare("
            UPDATE admin_users 
            SET reset_token=?, token_expiry=? 
            WHERE admin_email=?
        ");
        $update->bind_param("sss", $token, $expiry, $email);
        $update->execute();
        $update->close();

        $resetLink = "https://pritsco.com/android-apps/tester/admin/reset_password.php?token=".$token;

        /* ===== SEND RESET EMAIL USING PHP mail() ===== */

        $subject = "Password Reset Request - GoMAPS";

        $messageBody = "
        <html>
        <head>
        <title>Password Reset</title>
        </head>
        <body style='font-family:Arial;'>
        <h3>Password Reset Request</h3>
        <p>You requested to reset your password.</p>
        <p>Click below link to reset password:</p>
        <p><a href='$resetLink'>$resetLink</a></p>
        <br>
        <p>This link expires in 1 hour.</p>
        </body>
        </html>
        ";

        $headers  = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        $headers .= "From: admin@pritsco.com" . "\r\n";
        $headers .= "Reply-To: admin@pritsco.com" . "\r\n";

        if(mail($email, $subject, $messageBody, $headers)){
            $message = "Reset link sent successfully to your email.";
        } else {
            $error = "Mail sending failed. Please contact hosting provider.";
        }

    } else {
        // Security: Don't reveal whether email exists
        $message = "If the email exists, reset link has been sent.";
    }

    $stmt->close();
}
?>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Forgot Password</title>

<style>
*{ box-sizing:border-box; }

body{
    margin:0;
    font-family:'Segoe UI', sans-serif;
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
    background:linear-gradient(135deg,#007185,#003f46);
}

.login-container{
    background:#ffffff;
    width:100%;
    max-width:380px;
    padding:40px 30px;
    border-radius:15px;
    box-shadow:0 15px 40px rgba(0,0,0,0.2);
}

.login-container h2{
    text-align:center;
    margin-bottom:25px;
    color:#007185;
}

.input-group{
    margin-bottom:18px;
}

.input-group input{
    width:100%;
    padding:12px 14px;
    border-radius:8px;
    border:1px solid #ccc;
    font-size:14px;
    outline:none;
    transition:0.2s;
}

.input-group input:focus{
    border-color:#007185;
    box-shadow:0 0 5px rgba(0,113,133,0.3);
}

.login-btn{
    width:100%;
    padding:12px;
    border:none;
    border-radius:8px;
    background:#007185;
    color:#fff;
    font-size:15px;
    cursor:pointer;
    transition:0.2s;
}

.login-btn:hover{
    background:#005f68;
}

.error{
    color:red;
    font-size:14px;
    text-align:center;
    margin-bottom:15px;
}

.success{
    color:green;
    font-size:14px;
    text-align:center;
    margin-bottom:15px;
}

@media(max-width:480px){
    .login-container{
        margin:20px;
        padding:30px 20px;
    }
}
</style>
</head>
<body>

<div class="login-container">

<h2>🔐 Forgot Password</h2>

<?php if($error): ?>
<div class="error"><?= $error ?></div>
<?php endif; ?>

<?php if($message): ?>
<div class="success"><?= $message ?></div>
<?php endif; ?>

<form method="POST">

<div class="input-group">
<input type="email" name="email" placeholder="Enter Admin Email" required>
</div>

<button type="submit" class="login-btn">
Generate Reset Link
</button>

<p style="text-align:center;margin-top:15px;">
<a href="login.php" style="color:#007185;text-decoration:none;">
Back to Login
</a>
</p>

</form>

</div>

</body>
</html>