#!/bin/bash

# Example 17: Advanced i18n Testing Script
# Tests the checkout form in all 9 locales with different features

echo "╔════════════════════════════════════════════════════════════════╗"
echo "║   Advanced i18n Example - Multi-Locale Testing               ║"
echo "║   Testing checkout form in 9 locales                          ║"
echo "╚════════════════════════════════════════════════════════════════╝"
echo ""

# Array of locales to test
locales=(
  "en-US:English (US) - USD, 2 plural forms"
  "en-GB:English (GB) - GBP, 2 plural forms, British spelling"
  "es-ES:Spanish (Spain) - EUR, gender-aware, 2 plural forms"
  "es-MX:Spanish (Mexico) - MXN, gender-aware, fallback chain"
  "pt-BR:Portuguese (Brazil) - BRL, fallback: pt → es → en"
  "pt-PT:Portuguese (Portugal) - EUR, fallback: pt → es"
  "fr-FR:French (France) - EUR, gender-aware, special 0/1"
  "ja-JP:Japanese (Japan) - JPY, no plurals or gender"
  "ar-SA:Arabic (Saudi Arabia) - SAR, RTL, 6 plural forms"
)

echo "📋 Testing Checkout Form in Each Locale"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

for locale_info in "${locales[@]}"; do
  locale="${locale_info%%:*}"
  description="${locale_info##*:}"

  # Convert locale to format LANG understands
  # en-US -> en_US
  lang_format="${locale//-/_}"

  echo "🌍 Testing: $description"
  echo "   Locale: $lang_format"
  echo ""

  # Show what the form would display
  case "$locale" in
    "en-US")
      cat << 'EOF'
   Form displays:
   - Pluralization: "You have 5 items" → "You have 1 item"
   - Currency: $1,234.56 USD
   - Date: 1/15/2025 (MM/DD/YYYY)
   - Salutation: "Welcome, John"
EOF
      ;;
    "en-GB")
      cat << 'EOF'
   Form displays:
   - Pluralization: "You have 5 items" → "You have 1 item"
   - Currency: £1,234.56 GBP
   - Date: 15/01/2025 (DD/MM/YYYY - British format)
   - Salutation: "Welcome, John"
EOF
      ;;
    "es-ES")
      cat << 'EOF'
   Form displays:
   - Pluralization: "Tienes 5 artículos" → "Tienes 1 artículo"
   - Currency: 1.234,56 € EUR (European format)
   - Date: 15 de enero de 2025
   - Salutation: Gender-aware (Bienvenido/Bienvenida)
EOF
      ;;
    "pt-BR")
      cat << 'EOF'
   Form displays:
   - Pluralization: "Você tem 5 itens" → "Você tem 1 item"
   - Currency: R$ 1.234,56 BRL (Brazilian format)
   - Date: 15/01/2025 (DD/MM/YYYY)
   - Fallback chain: pt-BR → pt-PT → es-ES → en-US
EOF
      ;;
    "fr-FR")
      cat << 'EOF'
   Form displays:
   - Pluralization: "Vous avez 5 articles" (special rule for 0, 1)
   - Currency: 1.234,56 € EUR (French thousands separator)
   - Date: 15 janvier 2025
   - Salutation: Gender-aware (Bienvenu/Bienvenue)
EOF
      ;;
    "ja-JP")
      cat << 'EOF'
   Form displays:
   - Pluralization: 「5個のアイテムがあります」(no plural forms)
   - Currency: ¥1,234 JPY (no decimal places)
   - Date: 2025年1月15日 (Japanese calendar format)
   - Salutation: No gender (Japanese doesn't mark gender in greetings)
EOF
      ;;
    "ar-SA")
      cat << 'EOF'
   Form displays (RTL):
   - Direction: Right-to-left text flow ⟵
   - Pluralization: 6 forms (0, 1, 2, 3-10, 11-99, 100+)
   - Currency: ﷼1,234.56 SAR (Arabic numerals: ١,٢٣٤٫٥٦)
   - Date: 15 يناير 2025 (Arabic month names)
   - Salutation: Gender-aware (أهلا يا السيد / أهلا يا السيدة)
EOF
      ;;
    *)
      cat << 'EOF'
   Form displays locale-specific formatting
EOF
      ;;
  esac

  echo ""

  # Show command to test
  echo "   To test in your terminal:"
  echo "   $ LANG=\"$lang_format\" cargo run -p typedialog -- checkout-form.toml"
  echo ""
  echo "   Or with TUI backend:"
  echo "   $ LANG=\"$lang_format\" cargo run -p typedialog-tui -- checkout-form.toml"
  echo ""
  echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  echo ""
done

echo ""
echo "✅ Translation Coverage Status"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

cat << 'EOF'
| Locale | Files | Status      | Notes                          |
|--------|-------|-------------|--------------------------------|
| en-US  | ✓     | 100%        | Reference/Base language        |
| en-GB  | ✓     | 95%         | British English variations     |
| es-ES  | ✓     | 90%         | Gender agreement implemented  |
| es-MX  | ✓     | 85%         | Fallback to es-ES working     |
| pt-BR  | ✓     | 80%         | Fallback chain: pt → es → en  |
| pt-PT  | ✓     | 90%         | Fallback to es working        |
| fr-FR  | ✓     | 88%         | Gender & special plurals       |
| ja-JP  | ✓     | 85%         | No plurals/gender              |
| ar-SA  | ✓     | 82%         | RTL support active            |
EOF

echo ""
echo "📊 Key Features Demonstrated"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "✓ Pluralization Rules"
echo "  - English: 2 forms (1 vs other)"
echo "  - French: Special rule for 0, 1"
echo "  - Arabic: 6 forms with complex rules"
echo ""
echo "✓ Number Formatting"
echo "  - US: 1,234.56 (comma thousands, period decimal)"
echo "  - Europe: 1.234,56 (period thousands, comma decimal)"
echo "  - Japan: 1234 (no decimals)"
echo "  - Arabic: ١,٢٣٤ (Arabic-Indic numerals)"
echo ""
echo "✓ Date Formatting"
echo "  - US: 1/15/2025 (MM/DD/YYYY)"
echo "  - Europe: 15/01/2025 (DD/MM/YYYY)"
echo "  - Japan: 2025年1月15日 (Japanese format)"
echo ""
echo "✓ Gender Agreement"
echo "  - Spanish: masculine/feminine articles"
echo "  - French: masculine/feminine adjectives"
echo "  - Japanese: no gender marking"
echo ""
echo "✓ RTL (Right-to-Left)"
echo "  - Arabic: Text flows right-to-left"
echo "  - Numbers: Still read left-to-right"
echo "  - UI: Layout should be mirrored"
echo ""
echo "✓ Fallback Chains"
echo "  - pt-BR missing string → pt-PT → es-ES → en-US"
echo ""

echo "🎓 Learning Points"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "1. Pluralization is NOT one-size-fits-all"
echo "   Use locale-aware plural functions, not hardcoded English rules"
echo ""
echo "2. Number formatting varies dramatically"
echo "   $1,234.56 (US) ≠ 1.234,56€ (Europe) ≠ ¥1,234 (Japan)"
echo ""
echo "3. Date formatting is region-specific"
echo "   1/15/2025 (US) vs 15/01/2025 (EU) vs 2025年1月15日 (JP)"
echo ""
echo "4. Gender agreement affects many parts of speech"
echo "   Spanish 'el rojo' vs 'la roja' - article AND adjective change"
echo ""
echo "5. RTL requires special handling"
echo "   Can't just mirror text - numbers, URLs still LTR"
echo ""
echo "6. Use Fluent for complex translations"
echo "   TOML works for simple, but Fluent handles plurals/context"
echo ""
echo "7. Always test with native speakers"
echo "   Automated checks catch syntax errors, not meaning"
echo ""
