#!/usr/bin/env python3
"""
Fix submenu translations in Arabic pages
"""

from pathlib import Path
from bs4 import BeautifulSoup

def fix_submenus(html_file):
    """Fix submenu translations"""
    html_path = Path(html_file)
    if not html_path.exists():
        return False
    
    with open(html_path, 'r', encoding='utf-8', errors='ignore') as f:
        content = f.read()
    
    soup = BeautifulSoup(content, 'html.parser')
    changed = False
    
    # Find all navigation menus
    navs = soup.find_all('nav') + soup.find_all('div', class_='mobile-sidebar-nav')
    
    for nav in navs:
        # Find "About Us" submenu
        about_links = nav.find_all('a', href=lambda x: x and 'about-ar.html' in str(x))
        for link in about_links:
            href = link.get('href', '')
            text = link.get_text(strip=True)
            
            if '#certificates' in href and text != 'الشهادات':
                link.string = 'الشهادات'
                changed = True
            elif '#clients' in href and text != 'العملاء':
                link.string = 'العملاء'
                changed = True
            elif 'about-ar.html' == href and text == 'من نحن' and link.parent.name == 'li':
                # Check if it's a submenu item (not main menu)
                parent_ul = link.find_parent('ul')
                if parent_ul and ('submenu' in str(parent_ul.get('class', [])) or link.parent.find_parent('ul')):
                    link.string = 'عن السيف'
                    changed = True
        
        # Find "Services" submenu - need to find each ul separately
        service_menus = nav.find_all('ul')
        service_names = ["البنية التحتية", "المباني", "المقاولات العامة", "التصميم والبناء", "إدارة المشاريع"]
        
        for ul in service_menus:
            # Check if this is a services submenu
            service_links = ul.find_all('a', href=lambda x: x and 'ourServicedetail-ar.html' in str(x))
            if len(service_links) >= 3:  # Likely a services submenu
                for i, link in enumerate(service_links):
                    if i < len(service_names):
                        current_text = link.get_text(strip=True)
                        # Only update if it's wrong
                        if current_text != service_names[i]:
                            link.string = service_names[i]
                            changed = True
    
    if changed:
        with open(html_path, 'w', encoding='utf-8') as f:
            f.write(str(soup))
        print(f"✓ Fixed {html_file}")
        return True
    
    return False

if __name__ == "__main__":
    arabic_pages = [
        'home-ar.html',
        'about-ar.html',
        'ourService-ar.html',
        'ourServicedetail-ar.html',
        'project-ar.html',
        'projectDetail-ar.html',
        'Latest news-ar.html',
        'lastNewsDetails-ar.html',
        'join-ar.html',
        'contactUs-ar.html',
        'search-ar.html',
        'PrivacyPolicy-ar.html',
    ]
    
    fixed = 0
    for page in arabic_pages:
        if fix_submenus(page):
            fixed += 1
    
    print(f"\nFixed {fixed} files")
