import glob
import re

# 1. Fix wrong image in Chef Kamil index.html
ck_index_path = 'chef-kamil/index.html'
with open(ck_index_path, 'r', encoding='utf-8') as f:
    content = f.read()

content = content.replace('https://cdn.tgoapps.com/tgo2/spm/prod/meal/media/images/product/322551/10212628_1733140561476.jpg', 'images/171471784666348496b04d4663484a8b0ef2.jpg')

with open(ck_index_path, 'w', encoding='utf-8') as f:
    f.write(content)

# 2. Remove WhatsApp button from all files and inject 3 internal links into blog posts
def process_files():
    for site in ['tostum', 'chef-kamil']:
        html_files = glob.glob(f"{site}/**/*.html", recursive=True)
        for path in html_files:
            with open(path, 'r', encoding='utf-8') as f:
                html = f.read()
            
            # Remove whatsapp button (Regex to catch the block)
            # The button might be multiline
            html = re.sub(r'<a[^>]*class="whatsapp-btn"[^>]*>.*?</a>', '', html, flags=re.DOTALL)
            
            # Inject internal links to blog posts if it's a blog post (not index.html)
            if 'blog/' in path and not path.endswith('index.html'):
                # Check if we already have the related links block to avoid duplicates
                if 'seo-related-links' not in html:
                    injection = """
                    <div class="seo-related-links" style="margin-top: 30px; padding: 20px; background: rgba(128,128,128,0.1); border-radius: 8px;">
                        <h3>İlginizi Çekebilecek Diğer Sayfalarımız</h3>
                        <ul>
                            <li>Menümüzdeki tüm lezzetleri keşfetmek için <a href="../menu.html">Menü sayfamıza</a> göz atın.</li>
                            <li>Hakkımızda daha fazla bilgi almak isterseniz <a href="../hakkimizda.html">hikayemizi okuyabilirsiniz</a>.</li>
                            <li>Diğer yazılarımız için <a href="index.html">Blog sayfamızı</a> ziyaret edin veya hemen <a href="../index.html">ana sayfaya dönerek</a> detayları görün.</li>
                        </ul>
                    </div>
                    </article>
                    """
                    # Try to inject right before closing </article> or </main> or </div> before footer
                    if '</article>' in html:
                        html = html.replace('</article>', injection)
                    elif '</main>' in html:
                        html = html.replace('</main>', injection.replace('</article>', '</main>'))
                    else:
                        # Find the last closing div before footer
                        # A bit risky, but we can try before footer
                        injection = injection.replace('</article>', '')
                        html = html.replace('</section>', injection + '\n</section>', 1)
            
            with open(path, 'w', encoding='utf-8') as f:
                f.write(html)

process_files()
print("Fixes applied successfully.")
