// AUTO TESTER - Language System
const translations = {
    ar: {
        // Navigation
        'nav.home': 'Home',
        'nav.features': 'Features',
        'nav.types': 'Inspection Types',
        'nav.how': 'How It Works',
        'nav.faq': 'FAQ',
        'nav.login': 'Login',
        'nav.register': 'Register',
        'nav.about': 'About Us',
        'nav.contact': 'Contact',
        
        // Hero Section
        'hero.title': 'Artificial Intelligence<br>for Car Inspection in Egypt',
        'hero.subtitle': 'The first comprehensive Arabic car inspection system using AI technology. Get accurate reports and certified certificates within minutes.',
        'hero.btn.start': 'Start for Free',
        'hero.btn.ai': 'Try AI Experience',
        'hero.btn.more': 'Learn More',
        
        // Statistics
        'stats.inspections': 'Inspections Done',
        'stats.accuracy': 'Accuracy',
        'stats.points': 'Inspection Points',
        'stats.cities': 'Cities',
        
        // Features
        'features.title': 'Why AUTO TESTER?',
        'features.subtitle': 'We provide the best car inspection service in Egypt',
        'features.ai.title': 'AI Technology',
        'features.ai.desc': 'Advanced artificial intelligence for accurate diagnosis',
        'features.certified.title': 'Certified Reports',
        'features.certified.desc': 'Official certificates recognized by authorities',
        'features.fast.title': 'Fast Service',
        'features.fast.desc': 'Get results within minutes',
        'features.experts.title': 'Expert Team',
        'features.experts.desc': 'Professional inspectors with years of experience',
        
        // Inspection Types
        'types.title': 'Inspection Types',
        'types.subtitle': 'Choose the right inspection for your car',
        'types.comprehensive': 'Comprehensive',
        'types.comprehensive.desc': '200 inspection points',
        'types.computer': 'Computer Test',
        'types.computer.desc': 'OBD2 and electronic diagnosis',
        'types.dyno': 'Dyno Test',
        'types.dyno.desc': 'Power & torque measurement',
        'types.brake': 'Brake Test',
        'types.brake.desc': 'Brake system inspection',
        'types.ride': 'Ride Hailing',
        'types.ride.desc': 'Uber & Careem inspection',
        
        // How It Works
        'how.title': 'How It Works?',
        'how.subtitle': 'Three simple steps to inspect your car',
        'how.step1.title': 'Register Your Request',
        'how.step1.desc': 'Choose inspection type and enter your car details',
        'how.step2.title': 'We Perform Inspection',
        'how.step2.desc': 'Expert team inspects your car using latest equipment',
        'how.step3.title': 'Get Your Report',
        'how.step3.desc': 'Receive detailed report and certified certificate',
        
        // Testimonials
        'testimonials.title': 'What Our Clients Say?',
        'testimonials.subtitle': 'Client trust is our success secret',
        
        // FAQ
        'faq.title': 'Frequently Asked Questions',
        'faq.subtitle': 'Answers to all your questions',
        'faq.q1.question': 'How long does inspection take?',
        'faq.q1.answer': 'Duration varies by inspection type. Quick test takes 30 minutes, comprehensive 60-90 minutes.',
        'faq.q2.question': 'Is the certificate officially certified?',
        'faq.q2.answer': 'Yes, all our certificates are certified by the main center and can be verified via QR code.',
        'faq.q3.question': 'How do I get the report?',
        'faq.q3.answer': 'You can receive the report via WhatsApp, email, or download PDF from your account.',
        'faq.q4.question': 'What payment methods are available?',
        'faq.q4.answer': 'Cash, bank transfer, Vodafone Cash, or InstaPay.',
        
        // CTA Section
        'cta.title': 'Start Your Car Inspection <span>Today</span>',
        'cta.subtitle': 'Get accurate report and certified certificate in minutes',
        
        // Footer
        'footer.about': 'The first comprehensive Arabic car inspection system using artificial intelligence. We provide accurate and comprehensive inspection service with certified certificates.',
        'footer.quick_links': 'Quick Links',
        'footer.support': 'Support',
        'footer.contact': 'Contact Us',
        'footer.copyright': '© 2025 AUTO TESTER - All Rights Reserved',
        'footer.tax': 'Tax ID: 2545521152',
        
        // Contact Info
        'contact.address': 'Egypt - Cairo - New Cairo - Roxi - Thuman Bay Street',
        'contact.phone': '01018518599 - 01158588444',
        'contact.email': 'info@autotester-egypt.com',
        
        // Buttons
        'btn.login': 'Login',
        'btn.register': 'Register for Free',
        'btn.view_details': 'View Details',
        'btn.book_now': 'Book Now',
        'btn.learn_more': 'Learn More',
        
        // Meta
        'site.title': 'Car Inspection Egypt - AUTO TESTER - AI Car Inspection System',
        'site.description': 'The first comprehensive Arabic car inspection system using artificial intelligence. Get accurate reports and certified certificates within minutes. Over 15,000 inspections completed with 98% accuracy.',
        'site.keywords': 'car inspection, artificial intelligence, inspection certificate, Egypt, AUTO TESTER, comprehensive inspection, computer test, OBD2, dyno test'
    },
    en: {
        // Navigation
        'nav.home': 'Home',
        'nav.features': 'Features',
        'nav.types': 'Inspection Types',
        'nav.how': 'How It Works',
        'nav.faq': 'FAQ',
        'nav.login': 'Login',
        'nav.register': 'Register',
        'nav.about': 'About Us',
        'nav.contact': 'Contact',
        
        // Hero Section
        'hero.title': 'Artificial Intelligence<br>for Car Inspection in Egypt',
        'hero.subtitle': 'The first comprehensive Arabic car inspection system using AI technology. Get accurate reports and certified certificates within minutes.',
        'hero.btn.start': 'Start for Free',
        'hero.btn.ai': 'Try AI Experience',
        'hero.btn.more': 'Learn More',
        
        // Statistics
        'stats.inspections': 'Inspections Done',
        'stats.accuracy': 'Accuracy',
        'stats.points': 'Inspection Points',
        'stats.cities': 'Cities',
        
        // Features
        'features.title': 'Why AUTO TESTER?',
        'features.subtitle': 'We provide the best car inspection service in Egypt',
        'features.ai.title': 'AI Technology',
        'features.ai.desc': 'Advanced artificial intelligence for accurate diagnosis',
        'features.certified.title': 'Certified Reports',
        'features.certified.desc': 'Official certificates recognized by authorities',
        'features.fast.title': 'Fast Service',
        'features.fast.desc': 'Get results within minutes',
        'features.experts.title': 'Expert Team',
        'features.experts.desc': 'Professional inspectors with years of experience',
        
        // Inspection Types
        'types.title': 'Inspection Types',
        'types.subtitle': 'Choose the right inspection for your car',
        'types.comprehensive': 'Comprehensive',
        'types.comprehensive.desc': '200 inspection points',
        'types.computer': 'Computer Test',
        'types.computer.desc': 'OBD2 and electronic diagnosis',
        'types.dyno': 'Dyno Test',
        'types.dyno.desc': 'Power & torque measurement',
        'types.brake': 'Brake Test',
        'types.brake.desc': 'Brake system inspection',
        'types.ride': 'Ride Hailing',
        'types.ride.desc': 'Uber & Careem inspection',
        
        // How It Works
        'how.title': 'How It Works?',
        'how.subtitle': 'Three simple steps to inspect your car',
        'how.step1.title': 'Register Your Request',
        'how.step1.desc': 'Choose inspection type and enter your car details',
        'how.step2.title': 'We Perform Inspection',
        'how.step2.desc': 'Expert team inspects your car using latest equipment',
        'how.step3.title': 'Get Your Report',
        'how.step3.desc': 'Receive detailed report and certified certificate',
        
        // Testimonials
        'testimonials.title': 'What Our Clients Say?',
        'testimonials.subtitle': 'Client trust is our success secret',
        
        // FAQ
        'faq.title': 'Frequently Asked Questions',
        'faq.subtitle': 'Answers to all your questions',
        'faq.q1.question': 'How long does inspection take?',
        'faq.q1.answer': 'Duration varies by inspection type. Quick test takes 30 minutes, comprehensive 60-90 minutes.',
        'faq.q2.question': 'Is the certificate officially certified?',
        'faq.q2.answer': 'Yes, all our certificates are certified by the main center and can be verified via QR code.',
        'faq.q3.question': 'How do I get the report?',
        'faq.q3.answer': 'You can receive the report via WhatsApp, email, or download PDF from your account.',
        'faq.q4.question': 'What payment methods are available?',
        'faq.q4.answer': 'Cash, bank transfer, Vodafone Cash, or InstaPay.',
        
        // CTA Section
        'cta.title': 'Start Your Car Inspection <span>Today</span>',
        'cta.subtitle': 'Get accurate report and certified certificate in minutes',
        
        // Footer
        'footer.about': 'The first comprehensive Arabic car inspection system using artificial intelligence. We provide accurate and comprehensive inspection service with certified certificates.',
        'footer.quick_links': 'Quick Links',
        'footer.support': 'Support',
        'footer.contact': 'Contact Us',
        'footer.copyright': '© 2025 AUTO TESTER - All Rights Reserved',
        'footer.tax': 'Tax ID: 2545521152',
        
        // Contact Info
        'contact.address': 'Egypt - Cairo - New Cairo - Roxi - Thuman Bay Street',
        'contact.phone': '01018518599 - 01158588444',
        'contact.email': 'info@autotester-egypt.com',
        
        // Buttons
        'btn.login': 'Login',
        'btn.register': 'Register for Free',
        'btn.view_details': 'View Details',
        'btn.book_now': 'Book Now',
        'btn.learn_more': 'Learn More',
        
        // Meta
        'site.title': 'Car Inspection Egypt - AUTO TESTER - AI Car Inspection System',
        'site.description': 'The first comprehensive Arabic car inspection system using artificial intelligence. Get accurate reports and certified certificates within minutes. Over 15,000 inspections completed with 98% accuracy.',
        'site.keywords': 'car inspection, artificial intelligence, inspection certificate, Egypt, AUTO TESTER, comprehensive inspection, computer test, OBD2, dyno test'
    }
};

let currentLanguage = localStorage.getItem('language') || 'ar';

// Translation function
function t(key) {
    return translations[currentLanguage][key] || key;
}

// Set language
function setLanguage(lang) {
    currentLanguage = lang;
    localStorage.setItem('language', lang);
    document.documentElement.lang = lang === 'ar' ? 'ar' : 'en';
    document.documentElement.dir = lang === 'ar' ? 'rtl' : 'ltr';
    updatePageContent();
    updateLanguageButton();
}

// Update page content
function updatePageContent() {
    // Update elements with data-translate attribute
    document.querySelectorAll('[data-translate]').forEach(el => {
        const key = el.getAttribute('data-translate');
        const translation = t(key);
        
        if (el.tagName === 'INPUT' && el.type === 'text' || el.type === 'email' || el.type === 'tel') {
            el.placeholder = translation;
        } else if (el.tagName === 'INPUT' && el.type === 'submit') {
            el.value = translation;
        } else {
            el.textContent = translation;
        }
    });
    
    // Update page title and meta
    document.title = t('site.title');
    updateMetaTags();
}

// Update meta tags
function updateMetaTags() {
    const description = document.querySelector('meta[name="description"]');
    if (description) {
        description.content = t('site.description');
    }
    
    const keywords = document.querySelector('meta[name="keywords"]');
    if (keywords) {
        keywords.content = t('site.keywords');
    }
}

// Update language button
function updateLanguageButton() {
    const langBtn = document.getElementById('languageToggle');
    if (langBtn) {
        langBtn.innerHTML = currentLanguage === 'ar' ? 
            '<i class="fas fa-globe"></i> English' : 
            '<i class="fas fa-globe"></i> Arabic';
    }
}

// Toggle language
function toggleLanguage() {
    const newLang = currentLanguage === 'ar' ? 'en' : 'ar';
    setLanguage(newLang);
    
    // Track language change
    if (typeof gtag !== 'undefined') {
        gtag('event', 'language_change', {
            'event_category': 'User Interaction',
            'event_label': newLang
        });
    }
}

// Add language toggle button
function addLanguageToggle() {
    const navbar = document.querySelector('.nav-container') || document.querySelector('.navbar');
    if (navbar && !document.getElementById('languageToggle')) {
        const langBtn = document.createElement('button');
        langBtn.id = 'languageToggle';
        langBtn.className = 'lang-toggle';
        langBtn.setAttribute('aria-label', 'Toggle Language');
        langBtn.onclick = toggleLanguage;
        navbar.appendChild(langBtn);
    }
}

// Initialize language system
function initLanguageSystem() {
    setLanguage(currentLanguage);
    addLanguageToggle();
    
    // Add language change animation
    document.addEventListener('click', (e) => {
        if (e.target.id === 'languageToggle') {
            document.body.style.opacity = '0.8';
            setTimeout(() => {
                document.body.style.opacity = '1';
            }, 300);
        }
    });
}

// Auto-detect browser language
function detectBrowserLanguage() {
    const browserLang = navigator.language || navigator.userLanguage;
    if (browserLang.startsWith('en') && !localStorage.getItem('language')) {
        setLanguage('en');
    }
}

// Export functions for global use
window.t = t;
window.setLanguage = setLanguage;
window.toggleLanguage = toggleLanguage;

// Initialize when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
    detectBrowserLanguage();
    initLanguageSystem();
});

// Handle page visibility changes for language consistency
document.addEventListener('visibilitychange', () => {
    if (!document.hidden) {
        updatePageContent();
    }
});
