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

require_once 'db.php';

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

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

// Initialize variables
$errorMessage = "";
$successMessage = "";
$resetToken = $_GET['token'] ?? "";
$userId = null;

// ✅ VERIFY RESET TOKEN FROM MYSQL
$stmt = $pdo->prepare("SELECT user_id FROM users WHERE reset_token = ?");
$stmt->execute([$resetToken]);
$user = $stmt->fetch();

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

if (!$userId) {
    $errorMessage = "Invalid or expired reset token.";
}

// ✅ HANDLE PASSWORD RESET USING MYSQL
if ($_SERVER['REQUEST_METHOD'] === 'POST' && $userId) {
    $newPassword = trim($_POST['new_password']);

    if (!empty($newPassword)) {
        // ✅ Hash the new password
        $hashedPassword = password_hash($newPassword, PASSWORD_DEFAULT);
        
        // ✅ UPDATE USER PASSWORD & CLEAR RESET TOKEN
        $stmt = $pdo->prepare("UPDATE users SET edit_token = ?, reset_token = NULL WHERE user_id = ?");
        $stmt->execute([$hashedPassword, $userId]);

        if ($stmt->rowCount()) {
            $successMessage = "Your password has been reset! You can now log in.";
        } else {
            $errorMessage = "Failed to reset password. Please try again.";
        }
    } else {
        $errorMessage = "Password cannot be empty.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password</title>
<style>
    body { 
        font-family: Arial, sans-serif; 
        text-align: center; 
        padding: 20px; 
        <?php if (!empty($backgroundImage)): ?>
        background: url('<?= htmlspecialchars($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);
        border-radius: 8px;
        width: 80%;
        margin: 40px auto 10px;
        position: relative;
        bottom: 0;
        clear: both;
    }
</style>
</head>
<body>
<?php include 'google-translate-widget-front-end.php'; ?>
<?php if (!empty($logoImage)): ?>
    <div style="text-align: center; padding: 20px;">
        <img src="<?= htmlspecialchars($logoImage); ?>" alt="Logo" style="max-width: 200px; height: auto;">
    </div>
<?php endif; ?>

<div class="container">
    <h2>Reset Your Password</h2>

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

    <?php if ($successMessage): ?>
        <p class="success"> <?= htmlspecialchars($successMessage); ?> </p>
        <br>
        <a href="user-login.php">Go to Login</a>
    <?php else: ?>
        <p>Enter your new password below.</p>
        <form method="POST">
            <input type="password" name="new_password" placeholder="Enter New Password" required>
            <button type="submit">Reset Password</button>
        </form>
    <?php endif; ?>
</div>

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

</body>
</html>
