#!/usr/bin/env python3
"""
Final comprehensive translation script
Translates all English text in Arabic pages to Arabic
Ensures English pages have no Arabic text (except language switcher)
"""

from pathlib import Path
from bs4 import BeautifulSoup
import re

class FinalTranslator:
    def __init__(self):
        self.base_dir = Path(".")
        
        # English to Arabic translations
        self.en_to_ar = {
            "Building Great Projects Since 2008": "بناء مشاريع عظيمة منذ عام 2008",
            "Our Expertise": "خبرتنا",
            "Ongoing Projects": "المشاريع الجارية",
            "Our Clients": "عملاؤنا",
            "Latest News": "آخر الأخبار",
            "Contact Us": "اتصل بنا",
            "Who We Are": "من نحن",
            "Services": "خدماتنا",
            "Projects": "المشاريع",
            "Join Us": "انضم إلينا",
            "Home": "الرئيسية",
            "Construction Services": "خدمات الإنشاءات",
            "SMART PROJECTS CONSTRUCTION CO.": "شركة المشاريع الذكية للإنشاءات",
            "About SPCC": "عن الشركة",
            "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.":
            "تحتاج مساعدة؟ اتصل بنا خلال ساعات العمل.",
            "Explore our Latest News, Industry Insights, and Noteworthy Achievements":
            "استكشف آخر أخبارنا ورؤى الصناعة والإنجازات البارزة",
            "Explore all project": "استكشف جميع المشاريع",
            "View project": "عرض المشروع",
            "Read more": "اقرأ المزيد",
            "More About Us": "المزيد عنا",
            "Vision & Mission": "الرؤية والرسالة",
            "Offering Unmatched Results in Every Project.": "تقديم نتائج لا مثيل لها في كل مشروع.",
        }
        
    def translate_arabic_page(self, html_file):
        """Translate 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: {e}")
            return False
        
        soup = BeautifulSoup(html_content, 'html.parser')
        changed = False
        
        # Find all text-containing elements
        for element in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'a', 'button', 'label', 'li']):
            # Skip language switcher
            if element.find_parent('li', id='navigation-Ar'):
                continue
            if 'lang-switcher' in str(element.get('class', [])):
                continue
            if element.find_parent(class_='lang-switcher'):
                continue
            
            text = element.get_text(strip=True)
            
            # Exact match
            if text in self.en_to_ar:
                element.clear()
                element.string = self.en_to_ar[text]
                changed = True
            else:
                # Partial match for longer text
                for en_text, ar_text in self.en_to_ar.items():
                    if len(en_text) > 15 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
        
        # Handle button spans and paragraphs separately
        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.en_to_ar:
                    span.string = self.en_to_ar[text]
                    changed = True
            
            p_tag = btn.find('p')
            if p_tag:
                text = p_tag.get_text(strip=True)
                if text in self.en_to_ar:
                    p_tag.string = self.en_to_ar[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: {e}")
                return False
        
        return False
    
    def clean_english_page(self, html_file):
        """Remove 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: {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: {e}")
            return False
        
        soup = BeautifulSoup(html_content, 'html.parser')
        changed = False
        arabic_pattern = re.compile(r'[\u0600-\u06FF]+')
        
        ar_to_en = {
            "الرئيسية": "Home",
            "من نحن": "Who We Are",
            "خدماتنا": "Services",
            "المشاريع": "Projects",
            "آخر الأخبار": "Latest News",
            "انضم إلينا": "Join Us",
            "اتصل بنا": "Contact Us",
        }
        
        for element in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'span', 'a', 'button']):
            # 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
            if element.find_parent(class_='lang-switcher'):
                continue
            
            text = element.get_text(strip=True)
            if arabic_pattern.search(text):
                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: {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...")
        for page in arabic_pages:
            if self.translate_arabic_page(page):
                ar_count += 1
        
        print("\nCleaning English pages...")
        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 = FinalTranslator()
    translator.translate_all()
