<?php
require_once 'config.php';

$token = $_GET['token'] ?? null;
$message = '';
$message_type = '';
$show_form = false;

if (!$token) {
    $message = "Invalid password reset link.";
    $message_type = 'error';
} else {
    // जांचें कि टोकन वैध है और उसकी समय सीमा समाप्त नहीं हुई है
    $stmt = $conn->prepare("SELECT id FROM users WHERE reset_token = ? AND reset_token_expires_at > NOW()");
    $stmt->bind_param("s", $token);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows == 1) {
        $user = $result->fetch_assoc();
        $user_id = $user['id'];
        $show_form = true;

        // --- PASSWORD UPDATE LOGIC ---
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $new_password = $_POST['new_password'];
            $confirm_password = $_POST['confirm_password'];

            if ($new_password !== $confirm_password) {
                $message = "Passwords do not match.";
                $message_type = 'error';
            } else {
                $password_hash = password_hash($new_password, PASSWORD_DEFAULT);
                // टोकन को NULL करें ताकि इसे दोबारा इस्तेमाल न किया जा सके
                $update_stmt = $conn->prepare("UPDATE users SET password_hash = ?, reset_token = NULL, reset_token_expires_at = NULL WHERE id = ?");
                $update_stmt->bind_param("si", $password_hash, $user_id);
                if ($update_stmt->execute()) {
                    $message = "Your password has been reset successfully!";
                    $message_type = 'success';
                    $show_form = false; // पासवर्ड बदलने के बाद फॉर्म छिपा दें
                }
            }
        }
    } else {
        $message = "This password reset link is invalid or has expired.";
        $message_type = 'error';
    }
    $stmt->close();
}
$conn->close();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Reset Password - Digify OS</title>
    <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 flex items-center justify-center h-screen">
    <div class="w-full max-w-md bg-white rounded-lg shadow-md p-8">
        <h2 class="text-2xl font-bold text-center text-gray-800 mb-6">Set a New Password</h2>

        <?php if (!empty($message)): ?>
            <div class="mb-4 p-4 rounded <?php echo ($message_type == 'success') ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'; ?>">
                <?php echo htmlspecialchars($message); ?>
            </div>
        <?php endif; ?>
        
        <?php if ($show_form): ?>
            <form method="POST" action="reset-password.php?token=<?php echo htmlspecialchars($token); ?>">
                <div class="mb-4">
                    <label for="new_password" class="block text-sm font-medium text-gray-700">New Password</label>
                    <input type="password" name="new_password" id="new_password" required class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm">
                </div>
                <div class="mb-6">
                    <label for="confirm_password" class="block text-sm font-medium text-gray-700">Confirm New Password</label>
                    <input type="password" name="confirm_password" id="confirm_password" required class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm">
                </div>
                <div class="flex justify-end">
                    <button type="submit" class="w-full inline-flex items-center justify-center px-4 py-2 bg-blue-600 border border-transparent rounded-md font-semibold text-white hover:bg-blue-700">
                        Reset Password
                    </button>
                </div>
            </form>
        <?php else: ?>
            <div class="text-center mt-4">
                <a href="login.php" class="text-sm text-blue-600 hover:underline">Click here to Login</a>
            </div>
        <?php endif; ?>
    </div>
</body>
</html>