#!/usr/bin/env python3
"""
Comprehensive translation: Translate all English text in Arabic pages to Arabic
and ensure English pages have no Arabic text (except language switcher)
"""

from pathlib import Path
from bs4 import BeautifulSoup
import re

class ComprehensiveTranslator:
    def __init__(self):
        self.base_dir = Path(".")
        
        # Comprehensive English to Arabic translations
        self.translations = {
            # Headings
            "Building Great Projects Since 2008": "بناء مشاريع عظيمة منذ عام 2008",
            "Our Expertise": "خبرتنا",
            "Ongoing Projects": "المشاريع الجارية",
            "Our Clients": "عملاؤنا",
            "Our Traction": "تقدمنا",
            "Latest News": "آخر الأخبار",
            "Contact Us": "اتصل بنا",
            "Who We Are": "من نحن",
            "Services": "خدماتنا",
            "Projects": "المشاريع",
            "Join Us": "انضم إلينا",
            "Home": "الرئيسية",
            "Construction Services": "خدمات الإنشاءات",
            "SMART PROJECTS CONSTRUCTION CO.": "شركة المشاريع الذكية للإنشاءات",
            "About SPCC": "عن الشركة",
            
            # Content paragraphs
            "We have consistently demonstrated our ability to execute complex projects efficiently, adhering to strict timelines and budgetary constraints while maintaining the highest quality standards.": 
            "لقد أظهرنا باستمرار قدرتنا على تنفيذ المشاريع المعقدة بكفاءة، مع الالتزام بجداول زمنية صارمة وقيود مالية مع الحفاظ على أعلى معايير الجودة.",
            
            "SPCC is a quality-driven construction company founded by highly qualified and well-experienced engineers in all disciplines to stand up as an example of good engineering practice.":
            "شركة المشاريع الذكية للإنشاءات هي شركة إنشاءات مدفوعة بالجودة تأسست من قبل مهندسين مؤهلين تأهيلاً عالياً وذوي خبرة واسعة في جميع التخصصات لتكون مثالاً على الممارسة الهندسية الجيدة.",
            
            "Need help? Contact us during office hours.":
            "تحتاج مساعدة؟ اتصل بنا خلال ساعات العمل.",
            
            # Service descriptions
            "We specialize in constructing robust and efficient industrial facilities tailored to meet the specific needs of various industries. Our expertise encompasses everything from manufacturing plants and warehouses to distribution centers and industrial complexes.":
            "نتخصص في بناء المرافق الصناعية القوية والفعالة المصممة خصيصاً لتلبية الاحتياجات المحددة لمختلف الصناعات. تشمل خبرتنا كل شيء من مصانع التصنيع والمستودعات إلى مراكز التوزيع والمجمعات الصناعية.",
            
            "Our commercial building projects focus on creating functional and aesthetically pleasing spaces that drive business success. We deliver a wide range of commercial structures, including office buildings, retail centers, shopping malls, and mixed-use developments.":
            "تركز مشاريع المباني التجارية لدينا على إنشاء مساحات وظيفية وجذابة من الناحية الجمالية تحقق نجاح الأعمال. نقدم مجموعة واسعة من الهياكل التجارية، بما في ذلك المباني المكتبية ومراكز البيع بالتجزئة ومراكز التسوق والتطويرات متعددة الاستخدامات.",
            
            # Other phrases
            "Explore our Latest News, Industry Insights, and Noteworthy Achievements":
            "استكشف آخر أخبارنا ورؤى الصناعة والإنجازات البارزة",
            
            "Explore all project": "استكشف جميع المشاريع",
            "View project": "عرض المشروع",
            "Read more": "اقرأ المزيد",
            "More About Us": "المزيد عنا",
            "Vision & Mission": "الرؤية والرسالة",
        }
        
    def translate_arabic_page_content(self, html_file):
        """Translate all English content in Arabic pages"""
        html_path = self.base_dir / html_file
        if not html_path.exists():
            return False
        
        print(f"Translating: {html_file}...")
        
        try:
            with open(html_path, 'r', encoding='utf-8', errors='ignore') as f:
                html_content = f.read()
        except Exception as e:
            print(f"Error reading {html_file}: {e}")
            return False
        
        soup = BeautifulSoup(html_content, 'html.parser')
        changed = False
        
        # Translate all text elements
        for element in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'a', 'button', 'label']):
            # Skip language switcher and navigation language links
            if element.find_parent('li', id='navigation-Ar'):
                continue
            if 'lang-switcher' in str(element.get('class', [])):
                continue
            
            text = element.get_text(strip=True)
            
            # Check exact matches first
            if text in self.translations:
                element.clear()
                element.string = self.translations[text]
                changed = True
            else:
                # Check for partial matches in longer text
                for en_text, ar_text in self.translations.items():
                    if len(en_text) > 20 and en_text in text:
                        new_text = text.replace(en_text, ar_text)
                        if new_text != text:
                            element.clear()
                            element.string = new_text
                            changed = True
                            break
        
        # Translate button text (check spans and p tags inside buttons)
        buttons = soup.find_all('button')
        for btn in buttons:
            span = btn.find('span')
            if span:
                text = span.get_text(strip=True)
                if text in self.translations:
                    span.string = self.translations[text]
                    changed = True
            
            p_tag = btn.find('p')
            if p_tag:
                text = p_tag.get_text(strip=True)
                if text in self.translations:
                    p_tag.string = self.translations[text]
                    changed = True
        
        if changed:
            try:
                with open(html_path, 'w', encoding='utf-8') as f:
                    f.write(str(soup))
                print(f"✓ Translated {html_file}")
                return True
            except Exception as e:
                print(f"Error writing {html_file}: {e}")
                return False
        
        return False
    
    def clean_english_page(self, html_file):
        """Remove any Arabic text from English pages (except language switcher)"""
        html_path = self.base_dir / html_file
        if not html_path.exists():
            return False
        
        print(f"Cleaning English page: {html_file}...")
        
        try:
            with open(html_path, 'r', encoding='utf-8', errors='ignore') as f:
                html_content = f.read()
        except Exception as e:
            print(f"Error reading {html_file}: {e}")
            return False
        
        soup = BeautifulSoup(html_content, 'html.parser')
        changed = False
        arabic_pattern = re.compile(r'[\u0600-\u06FF]+')
        
        # Arabic to English mapping for common phrases
        ar_to_en = {
            "الرئيسية": "Home",
            "من نحن": "Who We Are",
            "خدماتنا": "Services",
            "المشاريع": "Projects",
            "آخر الأخبار": "Latest News",
            "انضم إلينا": "Join Us",
            "اتصل بنا": "Contact Us",
        }
        
        # Find and translate Arabic text (skip language switcher)
        for element in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'a']):
            # Skip language switcher
            if element.find_parent('li', id='navigation-Ar'):
                continue
            if 'lang-switcher' in str(element.get('class', [])) or 'ar' in str(element.get('class', [])):
                continue
            
            text = element.get_text(strip=True)
            if arabic_pattern.search(text):
                # Check if it's a known Arabic phrase
                if text in ar_to_en:
                    element.clear()
                    element.string = ar_to_en[text]
                    changed = True
        
        if changed:
            try:
                with open(html_path, 'w', encoding='utf-8') as f:
                    f.write(str(soup))
                print(f"✓ Cleaned {html_file}")
                return True
            except Exception as e:
                print(f"Error writing {html_file}: {e}")
                return False
        
        return False
    
    def translate_all(self):
        """Translate all pages"""
        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',
        ]
        
        english_pages = [
            'home.html',
            'about.html',
            'ourService.html',
            'ourServicedetail.html',
            'project.html',
            'projectDetail.html',
            'Latestnews.html',
            'lastNewsDetails.html',
            'join.html',
            'contactUs.html',
            'search.html',
            'PrivacyPolicy.html',
        ]
        
        ar_count = 0
        en_count = 0
        
        print("Translating Arabic pages (English -> Arabic)...")
        for page in arabic_pages:
            if self.translate_arabic_page_content(page):
                ar_count += 1
        
        print("\nCleaning English pages (Arabic -> English)...")
        for page in english_pages:
            if self.clean_english_page(page):
                en_count += 1
        
        print(f"\n{'='*60}")
        print(f"Translated {ar_count} Arabic pages")
        print(f"Cleaned {en_count} English pages")
        print(f"{'='*60}")

if __name__ == "__main__":
    translator = ComprehensiveTranslator()
    translator.translate_all()
