/**
 * Translation API Service
 * Provides access to Jingrow's backend translation system including:
 * - Country names (via Babel Locale.territories)
 * - Language names (from Language PageType)
 * - System translations
 */

import { api } from './common'

export interface LanguageInfo {
  language_code: string
  language_name: string
}

export interface LanguageData {
  default_language: string
  languages: string[]
  codes_to_names: Record<string, string>
}

export interface TranslatedCountries {
  [englishName: string]: string // e.g., { "China": "中国" }
}

/**
 * Load all available languages from the system
 * This calls jingrow.desk.page.setup_wizard.setup_wizard.load_languages
 * which returns languages from the Language PageType
 */
export async function loadLanguages(): Promise<LanguageData> {
  try {
    const response = await api.call('jingrow.desk.page.setup_wizard.setup_wizard.load_languages')
    return response.message || response
  } catch (error: any) {
    console.error('Failed to load languages:', error)
    // Return fallback
    return {
      default_language: 'English',
      languages: ['English', '简体中文'],
      codes_to_names: {
        'en': 'English',
        'zh': '简体中文'
      }
    }
  }
}

/**
 * Get translated country names based on current language
 * This uses Babel's Locale.territories on the backend
 * Returns a mapping of English country name -> Translated name
 */
export async function getTranslatedCountries(): Promise<TranslatedCountries> {
  try {
    const response = await api.call('jingrow.geo.country_info.get_translated_countries')
    return response.message || response || {}
  } catch (error: any) {
    console.error('Failed to get translated countries:', error)
    return {}
  }
}

/**
 * Get all translations for the current language
 * This includes country names, system messages, etc.
 */
export async function getAppTranslations(): Promise<Record<string, string>> {
  try {
    const response = await api.call('jingrow.translate.get_app_translations')
    return response.message || response || {}
  } catch (error: any) {
    console.error('Failed to get app translations:', error)
    return {}
  }
}

/**
 * Load translations for a specific language
 * This is useful when changing language in the setup wizard
 */
export async function loadMessages(language: string): Promise<string> {
  try {
    const response = await api.call('jingrow.desk.page.setup_wizard.setup_wizard.load_messages', {
      language
    })
    return response.message || response || language
  } catch (error: any) {
    console.error('Failed to load messages:', error)
    return language
  }
}

/**
 * Load user's country based on IP geolocation
 */
export async function loadCountryFromIP(): Promise<string | null> {
  try {
    const response = await api.call('jingrow.desk.page.setup_wizard.setup_wizard.load_country')
    return response.message || response || null
  } catch (error: any) {
    console.error('Failed to load country from IP:', error)
    return null
  }
}
