#!/bin/bash

echo "=== RE-TRANSLATING ALL LANGUAGES WITH INFORMAL STYLE ==="
echo ""
echo "This will backup and then re-translate all language files using informal addressing:"
echo "  - Dutch: 'je' (not 'u')"
echo "  - German: 'du' (not 'Sie')"
echo "  - French: 'tu' (not 'vous')"
echo "  - Spanish: 'tú' (not 'usted')"
echo ""

# Backup current translations
echo "Creating backups of current translations..."
cp resources/lang/nl.json resources/lang/nl.json.formal-backup
cp resources/lang/de.json resources/lang/de.json.formal-backup
cp resources/lang/es.json resources/lang/es.json.formal-backup
cp resources/lang/fr.json resources/lang/fr.json.formal-backup
echo "Backups created with .formal-backup extension"
echo ""

# Delete all translated files so AI translator sees them as "missing"
echo "Removing current translations to trigger full re-translation..."
rm resources/lang/nl.json
rm resources/lang/de.json
rm resources/lang/es.json
rm resources/lang/fr.json

# Create empty files
echo "{}" > resources/lang/nl.json
echo "{}" > resources/lang/de.json
echo "{}" > resources/lang/es.json
echo "{}" > resources/lang/fr.json
echo "Empty translation files created"
echo ""

# Translate each language sequentially
echo "Starting Dutch (nl) translation with informal style..."
php artisan ai-translator:translate-json --source=en --locale=nl --non-interactive --chunk=100 2>&1 | tee /tmp/retranslate-nl-informal.log
echo ""
echo "Dutch complete! Waiting 10 seconds before next language..."
sleep 10

echo ""
echo "Starting German (de) translation with informal style..."
php artisan ai-translator:translate-json --source=en --locale=de --non-interactive --chunk=100 2>&1 | tee /tmp/retranslate-de-informal.log
echo ""
echo "German complete! Waiting 10 seconds before next language..."
sleep 10

echo ""
echo "Starting Spanish (es) translation with informal style..."
php artisan ai-translator:translate-json --source=en --locale=es --non-interactive --chunk=100 2>&1 | tee /tmp/retranslate-es-informal.log
echo ""
echo "Spanish complete! Waiting 10 seconds before next language..."
sleep 10

echo ""
echo "Starting French (fr) translation with informal style..."
php artisan ai-translator:translate-json --source=en --locale=fr --non-interactive --chunk=100 2>&1 | tee /tmp/retranslate-fr-informal.log
echo ""

echo ""
echo "=== ALL TRANSLATIONS COMPLETE WITH INFORMAL STYLE ==="
echo ""

# Show final counts
echo "Final key counts:"
php -r "
\$en = json_decode(file_get_contents('resources/lang/en.json'), true);
\$nl = json_decode(file_get_contents('resources/lang/nl.json'), true);
\$de = json_decode(file_get_contents('resources/lang/de.json'), true);
\$es = json_decode(file_get_contents('resources/lang/es.json'), true);
\$fr = json_decode(file_get_contents('resources/lang/fr.json'), true);

echo \"  en: \" . count(\$en) . \" keys\n\";
echo \"  nl: \" . count(\$nl) . \" keys\n\";
echo \"  de: \" . count(\$de) . \" keys\n\";
echo \"  es: \" . count(\$es) . \" keys\n\";
echo \"  fr: \" . count(\$fr) . \" keys\n\";
"

echo ""
echo "Formal backups are available at:"
echo "  - resources/lang/nl.json.formal-backup"
echo "  - resources/lang/de.json.formal-backup"
echo "  - resources/lang/es.json.formal-backup"
echo "  - resources/lang/fr.json.formal-backup"
