<?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 - GoMAPS Admin</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<style>
:root{
    --primary:#007185;
    --primary-dark:#005f6f;
    --light-bg:#f1f5f9;
    --card-bg:#ffffff;
    --text:#111;
    --muted:#555;
    --border:#e5e7eb;
}

*{
    box-sizing:border-box;
}

body{
    margin:0;
    font-family:'Segoe UI',system-ui,-apple-system,sans-serif;
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
    background:linear-gradient(135deg,var(--primary),var(--primary-dark));
}

/* Wrapper */
.reset-wrapper{
    width:100%;
    max-width:420px;
    padding:20px;
}

/* Card */
.reset-card{
    background:var(--card-bg);
    padding:45px 35px;
    border-radius:18px;
    box-shadow:0 25px 60px rgba(0,0,0,0.25);
    text-align:center;
}

/* Logo */
.reset-logo{
    display:flex;
    justify-content:center;
    align-items:center;
    gap:10px;
    margin-bottom:20px;
}

.reset-logo img{
    width:45px;
    height:45px;
}

.reset-logo span{
    font-size:20px;
    font-weight:600;
    color:var(--primary);
}

/* Subtitle */
.reset-subtitle{
    font-size:14px;
    color:var(--muted);
    margin-bottom:25px;
}

/* Input Group */
.input-group{
    margin-bottom:18px;
    position:relative;
}

.input-group input{
    width:100%;
    padding:13px 15px 13px 40px;
    border-radius:10px;
    border:1px solid var(--border);
    font-size:14px;
    outline:none;
    transition:.2s ease;
}

.input-group i{
    position:absolute;
    left:14px;
    top:50%;
    transform:translateY(-50%);
    color:var(--muted);
    font-size:14px;
}

.input-group input:focus{
    border-color:var(--primary);
    box-shadow:0 0 0 3px rgba(0,113,133,0.15);
}

/* Button */
.reset-btn{
    width:100%;
    padding:12px;
    border:none;
    border-radius:10px;
    background:var(--primary);
    color:#fff;
    font-size:15px;
    font-weight:500;
    cursor:pointer;
    transition:.2s ease;
}

.reset-btn:hover{
    background:var(--primary-dark);
    transform:translateY(-2px);
}

/* Alerts */
.error{
    background:#ffe5e5;
    color:#d8000c;
    padding:10px;
    border-radius:8px;
    font-size:13px;
    margin-bottom:18px;
}

.success{
    background:#e6fffa;
    color:#00695c;
    padding:10px;
    border-radius:8px;
    font-size:13px;
    margin-bottom:18px;
}

/* Footer */
.reset-footer{
    margin-top:15px;
    font-size:13px;
    color:var(--muted);
}

.reset-footer a{
    color:var(--primary);
    text-decoration:none;
    font-weight:500;
}

.reset-footer a:hover{
    text-decoration:underline;
}

/* Mobile */
@media(max-width:480px){
    .reset-card{
        padding:35px 25px;
    }
}
</style>
</head>
<body>

<div class="reset-wrapper">

<div class="reset-card">

<div class="reset-logo">
    <img src="https://pritsco.com/android-apps/search-engine/assets/gomaps-logo.png" alt="GoMAPS Logo">
    <span>GoMAPS Admin</span>
</div>

<div class="reset-subtitle">
    Enter your admin email to receive a reset link
</div>

<?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">
<i class="fa-solid fa-envelope"></i>
<input type="email" name="email" placeholder="Admin Email" required>
</div>

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

<div class="reset-footer">
    <a href="login.php">Back to Login</a>
</div>

</form>

</div>
</div>

</body>
</html>