import os
import re

# Mapping for ServicesPreview since it has different titles/structure
preview_mapping = {
    "Ağır Nakliyat": "Nakliyat-Taşımacılık",
    "Mobil Vinç": "Vinç hizmetleri",
    "Sepetli Platform": "Sepetli platform hizmetleri"
}

preview_file = 'components/ServicesPreview.tsx'
if os.path.exists(preview_file):
    with open(preview_file, 'r', encoding='utf-8') as f:
        content = f.read()

    for title, subdir in preview_mapping.items():
        image_dir = os.path.join('public/images', subdir)
        if os.path.exists(image_dir):
            files = [f for f in os.listdir(image_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp'))]
            if files:
                image_path = f"/images/{subdir}/{files[0]}"
                escaped_title = re.escape(title)
                pattern = rf'(title:\s*"{escaped_title}",\s*description:\s*".*?",\s*image:\s*")([^"]+)(")'
                content = re.sub(pattern, rf'\1{image_path}\3', content)

    with open(preview_file, 'w', encoding='utf-8') as f:
        f.write(content)
    print("Updated ServicesPreview.tsx")

# Also update Hero.tsx with a local crane image
hero_file = 'components/Hero.tsx'
if os.path.exists(hero_file):
    with open(hero_file, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Use one of the crane images for Hero
    image_dir = 'public/images/Vinç hizmetleri'
    if os.path.exists(image_dir):
        files = [f for f in os.listdir(image_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp'))]
        if files:
            image_path = f"/images/Vinç hizmetleri/{files[0]}"
            content = re.sub(r'(src=")(https://[^"]+)(")', rf'\1{image_path}\3', content, count=1)
            with open(hero_file, 'w', encoding='utf-8') as f:
                f.write(content)
            print("Updated Hero.tsx")
