import re

mapping = {
    'uskudar-tost-kulturu.html': '../images/karışık.jpeg',
    'her-ogun-tost-yenir-mi.html': '../images/kavurmalı kaşarlı.jpeg',
    'en-iyi-tost-cesitleri.html': '../images/special.jpeg',
    'uskudar-kahvalti-mekanlari.html': '../images/kaşarlı.jpeg',
    'tost-tarihi-turkiyede.html': '../images/avokadolu.jpeg',
    'uskudar-paket-servis.html': '../images/gözleme.jpg'
}

# 1. Fix individual blog posts
for filename, img_path in mapping.items():
    filepath = f"tostum/blog/{filename}"
    with open(filepath, 'r', encoding='utf-8') as f:
        html = f.read()
    
    # We look for <img src="../images/dismekan.jpg" ...> in the <article>
    html = re.sub(r'<img[^>]*src="\.\./images/dismekan\.jpg"[^>]*>', 
                  f'<img src="{img_path}" alt="Blog Cover" style="width: 100%; height: 400px; object-fit: cover; border-radius: 12px; margin-bottom: 2rem;">', 
                  html)
    
    with open(filepath, 'w', encoding='utf-8') as f:
        f.write(html)

# 2. Fix the index.html
with open('tostum/blog/index.html', 'r', encoding='utf-8') as f:
    index_html = f.read()

# For index.html we can find the <a> tag and replace the image inside it
for filename, img_path in mapping.items():
    # regex to find the block for this specific post
    # <a href="uskudar-tost-kulturu.html"> ... <img src="../images/dismekan.jpg" ...>
    pattern = r'(<a href="' + re.escape(filename) + r'">\s*)<img[^>]*src="\.\./images/dismekan\.jpg"[^>]*>'
    replacement = r'\1<img src="' + img_path + r'" class="menu-card-img" alt="Thumbnail" loading="lazy" style="width: 100%; height: 200px; object-fit: cover; border-top-left-radius: 8px; border-top-right-radius: 8px;">'
    index_html = re.sub(pattern, replacement, index_html)

with open('tostum/blog/index.html', 'w', encoding='utf-8') as f:
    f.write(index_html)

print("Tostum blog covers updated.")
