<?php
// Enable error reporting (for debugging)
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once 'db.php';
require_once 'mailer.php';



// ✅ Fetch branding settings
$stmt = $pdo->query("SELECT background_image, logo_image, brand_name FROM branding WHERE id = 1");
$branding = $stmt->fetch();

// ✅ Define variables
$backgroundImage = $branding['background_image'] ?? '';
$logoImage = $branding['logo_image'] ?? '';


// Initialize variables
$errorMessage = "";
$successMessage = "";

// **HANDLE PASSWORD RESET REQUEST**
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email']);

// ✅ FIND USER BY EMAIL IN MYSQL
$stmt = $pdo->prepare("SELECT user_id FROM users WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();

$userFound = $user['user_id'] ?? null;


    if ($userFound) {
// ✅ GENERATE SECURE RESET TOKEN
$resetToken = bin2hex(random_bytes(16)); // 32-character secure token

// ✅ STORE RESET TOKEN IN MYSQL
$stmt = $pdo->prepare("UPDATE users SET reset_token = ? WHERE user_id = ?");
$stmt->execute([$resetToken, $userFound]);


        // Generate reset link
        $resetLink = "https://" . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/') . "/reset.php?token=" . $resetToken;

        // **SEND EMAIL WITH USER ID INCLUDED**
require_once 'send-email.php';

if (sendPasswordResetEmail($email, $userFound, $resetLink)) {

    $successMessage = "A password reset link has been sent to your email.";
} else {
    $errorMessage = "Failed to send email. Please try again.";
}

    } else {
        $errorMessage = "Email not found. Please check and try again.";
    }
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Forgot Password</title>
<style>
    body { 
        font-family: Arial, sans-serif; 
        text-align: center; 
        padding: 20px; 
        <?php if (!empty($backgroundImage)): ?>
        background: url('<?= $backgroundImage; ?>') no-repeat center center fixed;
        background-size: cover;
        <?php else: ?>
        background: #f8f9fa;
        <?php endif; ?>
    }
    
    .container { 
        width: 90%; 
        max-width: 500px; 
        margin: auto; 
        background: white; 
        padding: 20px; 
        border-radius: 10px; 
        box-shadow: 0px 0px 10px rgba(0,0,0,0.1); 
    }
    
    input, button { 
        width: 100%; 
        padding: 10px; 
        margin: 5px 0; 
        border-radius: 5px; 
        border: 1px solid #ccc; 
    }
    
    button { 
        background: blue; 
        color: white; 
        border: none; 
        cursor: pointer; 
    }
    
    button:hover { background: darkblue; }
    
    .error { color: red; }
    .success { color: green; }
    
    .footer {
    text-align: center;
    padding: 20px;
    font-size: 14px;
    color: white;
    background: rgba(0, 0, 0, 0.6); /* Semi-transparent */
    border-radius: 8px;
    width: 80%;
    margin: 40px auto 10px; /* Ensures spacing from other elements */
    position: relative;
    bottom: 0;
    clear: both; /* Forces it to clear any floating elements */
}



</style>

</head>
<body>
<?php include 'google-translate-widget-front-end.php'; ?>

<?php if (!empty($logoImage)): ?>
    <div style="text-align: center; padding: 20px;">
        <img src="<?= $logoImage; ?>" alt="Logo" style="max-width: 200px; height: auto;">
    </div>
<?php endif; ?>


<div class="container">
    <h2>Forgot Password</h2>
    <p>Enter your email to receive a password reset link.</p>

    <?php if ($errorMessage): ?>
        <p class="error"><?php echo htmlspecialchars($errorMessage); ?></p>
    <?php endif; ?>

    <?php if ($successMessage): ?>
        <p class="success"><?php echo htmlspecialchars($successMessage); ?></p>
    <?php endif; ?>

    <form method="POST">
        <input type="email" name="email" placeholder="Enter Your Email" required>
        <button type="submit">Send Reset Link</button>
    </form>

    <br>
    <a href="user-login.php">Back to Login</a>
</div>

<footer class="footer">
    &copy; <?= str_replace(['https://', 'http://', 'www.'], '', $_SERVER['HTTP_HOST']); ?>, All Rights Reserved.
</footer>

</body>
</html>
