import os
import shutil
import glob

downloads_dir = r"C:\Users\Abhiram P M\Downloads"
images_to_copy = [
    "ChatGPT Image Aug 18, 2026, 05_24_08 PM.png",
    "ChatGPT Image Aug 18, 2026, 05_48_39 PM.png",
    "ChatGPT Image Aug 18, 2026, 05_51_30 PM.png",
    "ChatGPT Image Aug 18, 2026, 05_56_24 PM.png"
]

assets_dir = r"assets\images\new_uploads"
os.makedirs(assets_dir, exist_ok=True)

# File mappings (old full path or URL string -> new relative path)
replacements = {}

for img_name in images_to_copy:
    src = os.path.join(downloads_dir, img_name)
    safe_name = img_name.replace(" ", "_").replace(",", "").replace(":", "")
    dest = os.path.join(assets_dir, safe_name)
    
    if os.path.exists(src):
        shutil.copy2(src, dest)
        print(f"Copied {img_name} to {dest}")
        
        # We want to replace these strings in HTML:
        # file:///C:/Users/Abhiram P M/Downloads/ChatGPT Image Aug 18, 2026, 05_24_08 PM.png
        # file:///C:/Users/Abhiram%20P%20M/Downloads/ChatGPT%20Image%20Aug%2018,%202026,%2005_24_08%20PM.png
        # C:\Users\Abhiram P M\Downloads\ChatGPT Image Aug 18, 2026, 05_24_08 PM.png
        # We'll just replace the base names with different encodings
        
        url_encoded_name = img_name.replace(" ", "%20")
        
        replacements[f"file:///C:/Users/Abhiram P M/Downloads/{img_name}"] = dest.replace("\\", "/")
        replacements[f"file:///C:/Users/Abhiram%20P%20M/Downloads/{url_encoded_name}"] = dest.replace("\\", "/")
        replacements[f"C:\\Users\\Abhiram P M\\Downloads\\{img_name}"] = dest.replace("\\", "/")
        
    else:
        print(f"NOT FOUND: {src}")

# Add the 'praying woman' path if it exists
# D:\iss\Franciscan Society\assets\images\praying-woman.png
praying_woman_src = r"assets\images\praying-woman.png"
replacements[praying_woman_src.replace("\\", "/")] = "assets/images/new_uploads/ChatGPT_Image_Aug_18_2026_05_56_24_PM.png"

# Now apply replacements across all html files
for filepath in glob.glob('*.html'):
    with open(filepath, 'r', encoding='utf-8') as f:
        content = f.read()
        
    new_content = content
    for old_str, new_str in replacements.items():
        new_content = new_content.replace(old_str, new_str)
        
    if new_content != content:
        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(new_content)
        print(f"Updated paths in {filepath}")

print("Fix completed.")
