import os
import glob
import re

template_dir = r"C:\xampp\htdocs\New_Stram_Site_web\templates"

translations = {
    r'>Accueil<': r">{{ 'menu.home'|trans }}<",
    r'>Qui sommes-nous<': r">{{ 'menu.about'|trans }}<",
    r'>Notre approche<': r">{{ 'menu.approach'|trans }}<",
    r'>Nos expertises<': r">{{ 'menu.expertises'|trans }}<",
    r'>Projets &amp; références<': r">{{ 'menu.projects'|trans }}<",
    r'>Projets & références<': r">{{ 'menu.projects'|trans }}<",
    r'>Sustainability<': r">{{ 'menu.sustainability'|trans }}<",
    r'>Carrières<': r">{{ 'menu.careers'|trans }}<",
    r'>Contact<': r">{{ 'menu.contact'|trans }}<",
}

# The language switcher HTML block
header_info_old = r'<div class="header_info">\s*</div>'
header_info_new = """<div class="header_info">
\t\t\t\t\t\t\t<ul class="top_social">
\t\t\t\t\t\t\t\t<li style="margin-left: 15px; display: inline-block;">
\t\t\t\t\t\t\t\t\t<a href="{{ path('app_switch_language', {'locale': 'fr'}) }}" style="color: {% if app.request.locale == 'fr' %}#F59E00{% else %}white{% endif %}; font-weight: bold; padding: 0 5px;">FR</a>| 
\t\t\t\t\t\t\t\t\t<a href="{{ path('app_switch_language', {'locale': 'en'}) }}" style="color: {% if app.request.locale == 'en' %}#F59E00{% else %}white{% endif %}; font-weight: bold; padding: 0 5px;">EN</a>
\t\t\t\t\t\t\t\t</li>
\t\t\t\t\t\t\t</ul>
\t\t\t\t\t\t</div>"""

def process_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as f:
        content = f.read()

    original_content = content
    
    # Simple replaces for menus
    for old, new in translations.items():
        content = re.sub(old, new, content, flags=re.IGNORECASE)

    # Add language switcher
    # Sometimes it's not empty, sometimes it has spaces
    # regex <div class="header_info">[\s]*</div>
    content = re.sub(r'<div class="header_info">[\s]*</div>', header_info_new, content)

    if content != original_content:
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(content)
        print(f"Updated: {file_path}")

for root, _, files in os.walk(template_dir):
    # Skip admin folder if preferred, or include it. I'll include all except adminstram maybe
    if 'adminstram' in root:
        continue
    for file in files:
        if file.endswith('.html.twig'):
            process_file(os.path.join(root, file))

print("Done replacing text with translations.")
