import csv
import os

from bot.bot_models.product import Product
from django.conf import settings

import re
from urllib.parse import urlparse, parse_qs

def extract_query_name(url: str) -> str:
    """
    Extracts the SearchText parameter from the URL and formats it for filenames.
    Example: "Racing+Bike" -> "Racing_Bike"
    """
    parsed_url = urlparse(url)
    query = parse_qs(parsed_url.query).get("SearchText", ["results"])[0]
    formatted_query = re.sub(r'\W+', '_', query)  # Replace non-alphanum with underscore
    return formatted_query.strip("_")