import os
import shutil
import subprocess

backup_dir = r"D:\iss\backup_extract"
target_dir = r"D:\iss\Franciscan Society"

# 1. Copy all HTML files from backup to target
for file in os.listdir(backup_dir):
    if file.endswith(".html"):
        src = os.path.join(backup_dir, file)
        dst = os.path.join(target_dir, file)
        shutil.copy2(src, dst)
        print(f"Restored {file}")

# 2. Run the scripts that were created today in chronological order
scripts = [
    "align_dropdowns.py",
    "sync_header.py",
    "solid_header.py",
    "create_pages.py",
    "fix_blog_menu.py",
    "fix_images.py"
]

for script in scripts:
    script_path = os.path.join(target_dir, script)
    if os.path.exists(script_path):
        print(f"\nRunning {script}...")
        try:
            result = subprocess.run(["python", script_path], cwd=target_dir, capture_output=True, text=True)
            print(result.stdout)
            if result.stderr:
                print("Error:", result.stderr)
        except Exception as e:
            print(f"Failed to run {script}: {e}")

# 3. Apply the final manual fixes that were done right before the disaster
import glob

# Replace video and welcome-mosaic.png in index.html
with open(os.path.join(target_dir, "index.html"), "r", encoding="utf-8") as f:
    index_content = f.read()

# Fix video
index_content = index_content.replace(
    'src="assets/video/hero-banner.mp4"',
    'src="file:///C:/Users/Abhiram P M/Downloads/Create_video_from_photos_1080p_202608181709.mp4"'
)
index_content = index_content.replace(
    'src="assets/video/hero-banner.webm"',
    'src="file:///C:/Users/Abhiram P M/Downloads/Create_video_from_photos_1080p_202608181709.mp4"'
)

# Fix particle in hero
# The previous state had a bible image. We need to wrap it and add the particle if not present
if "bible-shining-particle" not in index_content:
    old_img = '<img src="assets/images/bible.png"'
    if old_img in index_content:
        new_img = '<div style="position: relative; display: inline-block;">\n                    <div class="bible-shining-particle"></div>\n                    <img src="assets/images/bible.png"'
        index_content = index_content.replace(old_img, new_img)
        # close the div
        index_content = index_content.replace('alt="Holy Bible">', 'alt="Holy Bible">\n                </div>')

with open(os.path.join(target_dir, "index.html"), "w", encoding="utf-8") as f:
    f.write(index_content)

# Replace welcome-mosaic.jpg globally but CAREFULLY
for file in glob.glob(os.path.join(target_dir, "*.html")):
    with open(file, "r", encoding="utf-8") as f:
        content = f.read()
    if "welcome-mosaic.jpg" in content:
        content = content.replace(
            "assets/images/welcome-mosaic.jpg",
            "file:///C:/Users/Abhiram P M/Downloads/ChatGPT Image Aug 18, 2026, 05_24_08 PM.png"
        )
        content = content.replace("welcome-mosaic.jpg", "ChatGPT Image Aug 18, 2026, 05_24_08 PM.png")
        with open(file, "w", encoding="utf-8") as f:
            f.write(content)
        print(f"Updated image in {os.path.basename(file)}")

print("\nRecovery complete.")
