/* ========================================
   Internationalization (i18n) Module
   Supports DE (German) and EN (English)
   ======================================== */

const I18N = {
  de: {
    'nav.dashboard': 'Dashboard',
    'nav.fundamentals': '🟢 Grundlagen',
    'nav.core': '🔵 Kern-Rollen',
    'nav.expert': '⭐ Experte & Architekt',
    'nav.security': '🔐 Security & Identity',
    'nav.devops': '🔄 DevOps',
    'nav.ai': '🤖 AI & Machine Learning',
    'nav.data': '📊 Data & Analytics',
    'nav.infrastructure': '🌐 Infrastruktur & Spezial',
    'dashboard.title': 'Azure Zertifizierungs-Portal',
    'dashboard.subtitle': 'Wähle eine Prüfung zum Lernen',
    'dashboard.totalProgress': 'Gesamtfortschritt',
    'dashboard.examsStarted': 'Prüfungen gestartet',
    'dashboard.questionsAnswered': 'Fragen beantwortet',
    'dashboard.avgScore': 'Ø Punktzahl',
    'exam.study': 'Lernmodus',
    'exam.quiz': 'Prüfungsmodus',
    'exam.domains': 'Themenbereiche',
    'exam.allQuestions': 'Alle Fragen',
    'exam.unanswered': 'Unbeantwortete',
    'exam.incorrect': 'Falsch beantwortete',
    'exam.bookmarked': 'Markierte',
    'exam.startQuiz': 'Prüfung starten',
    'exam.startStudy': 'Lernen starten',
    'exam.configQuiz': 'Prüfung konfigurieren',
    'exam.questionCount': 'Anzahl Fragen',
    'exam.passingScore': 'Bestehensgrenze',
    'exam.duration': 'Dauer',
    'exam.minutes': 'Minuten',
    'exam.level': 'Level',
    'exam.description': 'Beschreibung',
    'exam.attempts': 'Versuche',
    'exam.bestScore': 'Beste Punktzahl',
    'exam.mslearn': 'Microsoft Learn öffnen',
    'quiz.question': 'Frage',
    'quiz.of': 'von',
    'quiz.timeLeft': 'Verbleibende Zeit',
    'quiz.submit': 'Antwort prüfen',
    'quiz.next': 'Nächste',
    'quiz.previous': 'Vorherige',
    'quiz.finish': 'Prüfung beenden',
    'quiz.correct': '✓ Richtig!',
    'quiz.incorrect': '✗ Falsch!',
    'quiz.explanation': 'Erklärung',
    'quiz.hint': 'Hinweis',
    'quiz.reference': 'Referenz',
    'quiz.showAnswer': 'Antwort anzeigen',
    'quiz.hideAnswer': 'Antwort verbergen',
    'quiz.selectMultiple': 'Wähle alle zutreffenden Antworten',
    'quiz.selectOne': 'Wähle die richtige Antwort',
    'quiz.scenario': 'Szenario',
    'quiz.questionMap': 'Fragenübersicht',
    'quiz.endConfirm': 'Prüfung wirklich beenden? Du hast noch unbeantwortete Fragen.',
    'quiz.timeUp': 'Zeit abgelaufen!',
    'quiz.allQuestions': 'Alle Fragen',
    'quiz.numQuestions': 'Anzahl Fragen',
    'quiz.shuffle': 'Fragen mischen',
    'quiz.timed': 'Mit Timer',
    'quiz.start': 'Starten',
    'results.title': 'Ergebnisse',
    'results.passed': '🎉 Bestanden!',
    'results.failed': '📚 Nicht bestanden',
    'results.score': 'Punktzahl',
    'results.correct': 'Richtig',
    'results.incorrect': 'Falsch',
    'results.unanswered': 'Unbeantwortet',
    'results.review': 'Antworten überprüfen',
    'results.retry': 'Erneut versuchen',
    'results.back': 'Zurück zur Übersicht',
    'results.passingScore': 'Bestehensgrenze: 700/1000',
    'results.duration': 'Dauer',
    'filter.all': 'Alle',
    'filter.easy': 'Einfach',
    'filter.medium': 'Mittel',
    'filter.hard': 'Schwer',
    'filter.domain': 'Themenbereich',
    'filter.difficulty': 'Schwierigkeit',
    'filter.status': 'Status',
    'filter.answered': 'Beantwortet',
    'filter.unanswered': 'Unbeantwortet',
    'filter.correct': 'Richtig',
    'filter.incorrect': 'Falsch',
    'progress.reset': 'Fortschritt zurücksetzen',
    'progress.resetConfirm': 'Fortschritt für diese Prüfung wirklich zurücksetzen?',
    'progress.resetAll': 'Gesamten Fortschritt zurücksetzen',
    'progress.resetAllConfirm': 'Den gesamten Fortschritt für ALLE Prüfungen zurücksetzen?',
    'modal.close': 'Schließen',
    'level.fundamentals': 'Grundlagen',
    'level.associate': 'Associate',
    'level.expert': 'Experte',
    'loading': 'Laden...',
    'noQuestions': 'Keine Fragen gefunden für diese Filter.',
    'companies.title': 'Szenario-Unternehmen',
    'companies.subtitle': 'Diese fiktiven Unternehmen werden in den Prüfungsfragen als reale Szenarien verwendet',
    'companies.industry': 'Branche',
    'companies.hq': 'Hauptsitz',
    'companies.employees': 'Mitarbeiter',
    'companies.infrastructure': 'Azure-Infrastruktur',
    'companies.challenges': 'Aktuelle Herausforderungen',
    'companies.showAll': 'Alle Unternehmen anzeigen',
    'companies.showRelevant': 'Nur relevante anzeigen',
  },
  en: {
    'nav.dashboard': 'Dashboard',
    'nav.fundamentals': '🟢 Fundamentals',
    'nav.core': '🔵 Core Roles',
    'nav.expert': '⭐ Expert & Architect',
    'nav.security': '🔐 Security & Identity',
    'nav.devops': '🔄 DevOps',
    'nav.ai': '🤖 AI & Machine Learning',
    'nav.data': '📊 Data & Analytics',
    'nav.infrastructure': '🌐 Infrastructure & Specialty',
    'dashboard.title': 'Azure Certification Portal',
    'dashboard.subtitle': 'Choose an exam to study',
    'dashboard.totalProgress': 'Total Progress',
    'dashboard.examsStarted': 'Exams Started',
    'dashboard.questionsAnswered': 'Questions Answered',
    'dashboard.avgScore': 'Avg Score',
    'exam.study': 'Study Mode',
    'exam.quiz': 'Exam Mode',
    'exam.domains': 'Domains',
    'exam.allQuestions': 'All Questions',
    'exam.unanswered': 'Unanswered',
    'exam.incorrect': 'Incorrect',
    'exam.bookmarked': 'Bookmarked',
    'exam.startQuiz': 'Start Exam',
    'exam.startStudy': 'Start Studying',
    'exam.configQuiz': 'Configure Exam',
    'exam.questionCount': 'Number of Questions',
    'exam.passingScore': 'Passing Score',
    'exam.duration': 'Duration',
    'exam.minutes': 'Minutes',
    'exam.level': 'Level',
    'exam.description': 'Description',
    'exam.attempts': 'Attempts',
    'exam.bestScore': 'Best Score',
    'exam.mslearn': 'Open Microsoft Learn',
    'quiz.question': 'Question',
    'quiz.of': 'of',
    'quiz.timeLeft': 'Time Left',
    'quiz.submit': 'Check Answer',
    'quiz.next': 'Next',
    'quiz.previous': 'Previous',
    'quiz.finish': 'Finish Exam',
    'quiz.correct': '✓ Correct!',
    'quiz.incorrect': '✗ Incorrect!',
    'quiz.explanation': 'Explanation',
    'quiz.hint': 'Hint',
    'quiz.reference': 'Reference',
    'quiz.showAnswer': 'Show Answer',
    'quiz.hideAnswer': 'Hide Answer',
    'quiz.selectMultiple': 'Select all that apply',
    'quiz.selectOne': 'Select the correct answer',
    'quiz.scenario': 'Scenario',
    'quiz.questionMap': 'Question Map',
    'quiz.endConfirm': 'Really finish the exam? You still have unanswered questions.',
    'quiz.timeUp': 'Time is up!',
    'quiz.allQuestions': 'All Questions',
    'quiz.numQuestions': 'Number of Questions',
    'quiz.shuffle': 'Shuffle Questions',
    'quiz.timed': 'Timed',
    'quiz.start': 'Start',
    'results.title': 'Results',
    'results.passed': '🎉 Passed!',
    'results.failed': '📚 Failed',
    'results.score': 'Score',
    'results.correct': 'Correct',
    'results.incorrect': 'Incorrect',
    'results.unanswered': 'Unanswered',
    'results.review': 'Review Answers',
    'results.retry': 'Try Again',
    'results.back': 'Back to Overview',
    'results.passingScore': 'Passing Score: 700/1000',
    'results.duration': 'Duration',
    'filter.all': 'All',
    'filter.easy': 'Easy',
    'filter.medium': 'Medium',
    'filter.hard': 'Hard',
    'filter.domain': 'Domain',
    'filter.difficulty': 'Difficulty',
    'filter.status': 'Status',
    'filter.answered': 'Answered',
    'filter.unanswered': 'Unanswered',
    'filter.correct': 'Correct',
    'filter.incorrect': 'Incorrect',
    'progress.reset': 'Reset Progress',
    'progress.resetConfirm': 'Really reset progress for this exam?',
    'progress.resetAll': 'Reset All Progress',
    'progress.resetAllConfirm': 'Reset ALL progress for ALL exams?',
    'modal.close': 'Close',
    'level.fundamentals': 'Fundamentals',
    'level.associate': 'Associate',
    'level.expert': 'Expert',
    'loading': 'Loading...',
    'noQuestions': 'No questions found for these filters.',
    'companies.title': 'Scenario Companies',
    'companies.subtitle': 'These fictional companies are used in exam questions as real-world scenarios',
    'companies.industry': 'Industry',
    'companies.hq': 'Headquarters',
    'companies.employees': 'Employees',
    'companies.infrastructure': 'Azure Infrastructure',
    'companies.challenges': 'Current Challenges',
    'companies.showAll': 'Show all companies',
    'companies.showRelevant': 'Show relevant only',
  }
};

let currentLang = localStorage.getItem('certportal-lang') || 'de';

function t(key) {
  return (I18N[currentLang] && I18N[currentLang][key]) || (I18N['en'] && I18N['en'][key]) || key;
}

function setLanguage(lang) {
  currentLang = lang;
  localStorage.setItem('certportal-lang', lang);
  document.documentElement.lang = lang;

  // Update lang buttons
  const deBtn = document.getElementById('lang-de');
  const enBtn = document.getElementById('lang-en');
  if (deBtn) deBtn.classList.toggle('active', lang === 'de');
  if (enBtn) enBtn.classList.toggle('active', lang === 'en');

  // Update static i18n elements
  document.querySelectorAll('[data-i18n]').forEach(el => {
    el.textContent = t(el.dataset.i18n);
  });

  // Re-render current view if App is initialized
  if (typeof App !== 'undefined' && App.render) {
    App.render();
  }
}
