#!/usr/bin/env node
// https://www.permacomputer.com


/**
 * Script to add missing translation keys to all language files
 */

import { readFileSync, writeFileSync } from 'fs';
import { translateText } from '../translation.js';

const newKeys = {
	translatePageTab: "📄 Translate Page",
	customTextTab: "✏️ Custom Text",
	systemPromptIntro: "You are Hermes AI, powered by Nous Research, embedded on a webpage. Your task is to generate a warm, welcoming 3-paragraph introduction that shows you understand the specific page the user is viewing.",
	systemPromptTask1: "1. Introduces yourself as Hermes AI and acknowledges the specific page/content",
	systemPromptTask2: "2. Mentions 2-3 specific, relevant details you notice from the page content", 
	systemPromptTask3: "3. Offers to help with questions related to the page content",
	systemPromptInstructions: "Keep it conversational, helpful, and specific to what you see on this page. Write in"
};

const languages = [
	{ code: 'zh', name: 'Chinese (Simplified)' },
	{ code: 'hi', name: 'Hindi' },
	{ code: 'es', name: 'Spanish' },
	{ code: 'fr', name: 'French' },
	{ code: 'ar', name: 'Arabic' },
	{ code: 'bn', name: 'Bengali' },
	{ code: 'ru', name: 'Russian' },
	{ code: 'pt', name: 'Portuguese' },
	{ code: 'ur', name: 'Urdu' },
	{ code: 'id', name: 'Indonesian' },
	{ code: 'ja', name: 'Japanese' },
	{ code: 'sw', name: 'Swahili' },
	{ code: 'mr', name: 'Marathi' },
	{ code: 'te', name: 'Telugu' },
	{ code: 'tr', name: 'Turkish' },
	{ code: 'zh-tw', name: 'Chinese Traditional' },
	{ code: 'ko', name: 'Korean' }
];

async function addKeysToLanguage(langCode) {
	const filename = langCode === 'zh-tw' ? 'zh-tw.js' : `${langCode}.js`;
	const filePath = `./${filename}`;
	
	try {
		let content = readFileSync(filePath, 'utf8');
		
		// Find the insertion point (before "// More alerts and messages")
		const insertionPoint = content.indexOf('	// More alerts and messages');
		if (insertionPoint === -1) {
			console.log(`⚠️  Could not find insertion point in ${filename}`);
			return;
		}
		
		// Build the new keys section
		let newSection = '\n	// Translation modal tabs\n';
		newSection += `	translatePageTab: "${await translateText(newKeys.translatePageTab, langCode)}",\n`;
		newSection += `	customTextTab: "${await translateText(newKeys.customTextTab, langCode)}",\n\n`;
		
		newSection += '	// System prompt components\n';
		newSection += `	systemPromptIntro: "${await translateText(newKeys.systemPromptIntro, langCode)}",\n`;
		newSection += `	systemPromptTask1: "${await translateText(newKeys.systemPromptTask1, langCode)}",\n`;
		newSection += `	systemPromptTask2: "${await translateText(newKeys.systemPromptTask2, langCode)}",\n`;
		newSection += `	systemPromptTask3: "${await translateText(newKeys.systemPromptTask3, langCode)}",\n`;
		newSection += `	systemPromptInstructions: "${await translateText(newKeys.systemPromptInstructions, langCode)}",\n\n`;
		
		// Insert the new section
		const newContent = content.slice(0, insertionPoint) + newSection + content.slice(insertionPoint);
		
		writeFileSync(filePath, newContent, 'utf8');
		console.log(`✅ Updated ${filename}`);
		
	} catch (error) {
		console.log(`❌ Error updating ${filename}: ${error.message}`);
	}
}

async function main() {
	console.log('🔄 Adding missing translation keys to all language files...\n');
	
	for (const lang of languages) {
		console.log(`Processing ${lang.name} (${lang.code})...`);
		await addKeysToLanguage(lang.code);
		// Small delay to avoid overwhelming the translation API
		await new Promise(resolve => setTimeout(resolve, 1000));
	}
	
	console.log('\n🎉 Finished adding keys to all languages!');
}

// Export for testing
export default main;

main().catch(console.error);