<?php
// File: reset-password.php (Final Version)
include 'header.php';

// Validate the token from the URL
$token = $_GET['token'] ?? null;
$error_message = '';
$is_token_valid = false;

if ($token) {
    $stmt = $conn->prepare("SELECT * FROM password_resets WHERE token = ? AND expires_at > NOW()");
    $stmt->bind_param("s", $token);
    $stmt->execute();
    $result = $stmt->get_result();
    
    if ($result->num_rows > 0) {
        $is_token_valid = true;
    } else {
        $error_message = "This password reset link is invalid or has expired. Please request a new one.";
    }
    $stmt->close();
} else {
    $error_message = "No reset token provided.";
}
?>

<div class="min-h-screen flex items-center justify-center bg-stone-100 py-12 px-4 sm:px-6 lg:px-8">
    <div class="max-w-md w-full space-y-8 bg-white p-10 rounded-xl shadow-lg">
        <div>
            <h2 class="mt-6 text-center text-3xl font-extrabold text-ucf-charcoal">
                Set a New Password
            </h2>
        </div>

        <?php if (!$is_token_valid): ?>
            <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4" role="alert">
                <p class="font-bold">Error</p>
                <p><?php echo htmlspecialchars($error_message); ?></p>
            </div>
            <div class="text-sm text-center mt-4">
                <a href="forgot-password.php" class="font-medium text-ucf-green hover:text-ucf-green-dark">
                    Request another reset link
                </a>
            </div>
        <?php else: ?>
            <?php if (isset($_GET['error']) && $_GET['error'] == 'mismatch'): ?>
                <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4" role="alert">
                    <p class="font-bold">Error</p><p>The passwords you entered do not match.</p>
                </div>
            <?php endif; ?>

            <form class="mt-8 space-y-6" action="reset-password-handler.php" method="POST">
                <input type="hidden" name="token" value="<?php echo htmlspecialchars($token); ?>">
                <div class="rounded-md shadow-sm space-y-4">
                    <div>
                        <label for="new_password" class="sr-only">New Password</label>
                        <input id="new_password" name="new_password" type="password" required class="appearance-none rounded-lg relative block w-full px-4 py-3 border border-gray-300" placeholder="Enter new password">
                    </div>
                    <div>
                        <label for="confirm_password" class="sr-only">Confirm New Password</label>
                        <input id="confirm_password" name="confirm_password" type="password" required class="appearance-none rounded-lg relative block w-full px-4 py-3 border border-gray-300" placeholder="Confirm new password">
                    </div>
                </div>
                <div>
                    <button type="submit" class="group relative w-full flex justify-center py-3 px-4 text-sm font-medium rounded-lg text-white bg-ucf-green hover:bg-ucf-green-dark">
                        Reset Password
                    </button>
                </div>
            </form>
        <?php endif; ?>
    </div>
</div>

<?php include 'footer.php'; ?>

