false, 'message' => 'Please login first']); exit; } $action = $_POST['action'] ?? ''; $user_id = $_SESSION['user_id']; $product_id = intval($_POST['product_id']); switch ($action) { case 'toggle': if (in_wishlist($user_id, $product_id)) { remove_from_wishlist($user_id, $product_id); echo json_encode([ 'success' => true, 'action' => 'removed', 'count' => count(get_user_wishlist($user_id)) ]); } else { add_to_wishlist($user_id, $product_id); echo json_encode([ 'success' => true, 'action' => 'added', 'count' => count(get_user_wishlist($user_id)) ]); } break; case 'add': add_to_wishlist($user_id, $product_id); echo json_encode([ 'success' => true, 'message' => 'Added to wishlist', 'count' => count(get_user_wishlist($user_id)) ]); break; case 'remove': remove_from_wishlist($user_id, $product_id); echo json_encode([ 'success' => true, 'message' => 'Removed from wishlist', 'count' => count(get_user_wishlist($user_id)) ]); break; case 'check': echo json_encode([ 'success' => true, 'in_wishlist' => in_wishlist($user_id, $product_id) ]); break; default: echo json_encode(['success' => false, 'message' => 'Invalid action']); }