# textdocs/utils.py
import os
import logging
from django.conf import settings

logger = logging.getLogger(__name__)

def convert_media_urls_to_file_paths(html_string):
    """
    Replaces relative media URLs with absolute filesystem paths
    for WeasyPrint to read directly from disk.
    """
    media_url = settings.MEDIA_URL
    media_root = str(settings.MEDIA_ROOT)

    html_string = html_string.replace(
        f'src="{media_url}',
        f'src="file://{media_root}/'
    )
    html_string = html_string.replace(
        f"src='{media_url}",
        f"src='file://{media_root}/"
    )

    return html_string