import requests
import yaml
import os

# Load settings
with open("config/settings.yml", "r") as file:
    settings = yaml.safe_load(file)
HEADLESS_MODE_HTML = settings["debug"]["headless_mode"].get("html", True)  # Default to True

def fetch_html(url, debug=False):
    """Fetches static HTML pages using requests (no Playwright needed yet)."""
    response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"})
    response.raise_for_status()
    content = response.text

    if debug:
        save_debug_file(url, content, "html")

    return content

import os

def save_debug_file(site_name, content, file_type):
    """Saves fetched content for debugging, ensuring a clean filename."""
    safe_name = (
        site_name.replace("https://", "")
        .replace("http://", "")
        .replace("/", "_")
        .replace(".", "_")  # Normalize periods
        .rstrip("_")  # Remove trailing underscores
    )
    
    file_path = f"tests/{safe_name}.{file_type}"

    with open(file_path, "w", encoding="utf-8") as f:
        f.write(content)

    print(f"✅ Debug file saved: {file_path}")


