#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
LOCALES_FILE="$ROOT_DIR/assets/i18n/locales.json"
SOURCE_FILE="$ROOT_DIR/assets/i18n/en.json"

log() {
  printf '[i18n] %s\n' "$*"
}

fail() {
  printf '[i18n] error: %s\n' "$*" >&2
  exit 1
}

ensure_codex() {
  if command -v codex >/dev/null 2>&1; then
    return
  fi

  if command -v npm >/dev/null 2>&1; then
    log "installing Codex CLI via npm"
    npm i -g @openai/codex || fail "failed to install Codex CLI via npm"
  elif command -v brew >/dev/null 2>&1; then
    log "installing Codex CLI via brew"
    brew install codex || fail "failed to install Codex CLI via brew"
  else
    fail "Codex CLI not found and no supported installer available (npm or brew)"
  fi
}

ensure_codex_login() {
  if codex login status >/dev/null 2>&1; then
    return
  fi

  log "Codex login status unavailable or not logged in; starting login flow"
  codex login || fail "Codex login failed"
}

probe_translator() {
  command -v greentic-i18n-translator >/dev/null 2>&1 || fail "greentic-i18n-translator not found. Install it and rerun this script."

  local help_output
  help_output="$(greentic-i18n-translator --help 2>&1 || true)"
  [[ -n "$help_output" ]] || fail "unable to inspect greentic-i18n-translator --help"

  for cmd in translate; do
    if ! greentic-i18n-translator "$cmd" --help >/dev/null 2>&1; then
      fail "translator subcommand '$cmd' is required but unavailable"
    fi
  done
}

run_translate() {
  while IFS= read -r locale; do
    [[ -n "$locale" ]] || continue
    log "translating locale: $locale"
    greentic-i18n-translator translate \
      --langs "$locale" \
      --en "$SOURCE_FILE" || fail "translate failed for locale $locale"
  done < <(python3 - "$LOCALES_FILE" <<'PY'
import json
import sys
with open(sys.argv[1], 'r', encoding='utf-8') as f:
    data = json.load(f)
for locale in data:
    if locale != "en":
        print(locale)
PY
)
}

run_validate_per_locale() {
  local failed=0
  while IFS= read -r locale; do
    [[ -n "$locale" ]] || continue
    if ! greentic-i18n-translator validate --langs "$locale" --en "$SOURCE_FILE"; then
      log "validate failed for locale: $locale"
      failed=1
    fi
  done < <(python3 - "$LOCALES_FILE" <<'PY'
import json
import sys
with open(sys.argv[1], 'r', encoding='utf-8') as f:
    data = json.load(f)
for locale in data:
    if locale != "en":
        print(locale)
PY
)
  return "$failed"
}

run_status_per_locale() {
  local failed=0
  while IFS= read -r locale; do
    [[ -n "$locale" ]] || continue
    if ! greentic-i18n-translator status --langs "$locale" --en "$SOURCE_FILE"; then
      log "status failed for locale: $locale"
      failed=1
    fi
  done < <(python3 - "$LOCALES_FILE" <<'PY'
import json
import sys
with open(sys.argv[1], 'r', encoding='utf-8') as f:
    data = json.load(f)
for locale in data:
    if locale != "en":
        print(locale)
PY
)
  return "$failed"
}

run_optional_checks() {
  if greentic-i18n-translator validate --help >/dev/null 2>&1; then
    log "running translator validate"
    if ! run_validate_per_locale; then
      fail "translator validate failed"
    fi
  else
    log "warning: translator validate command not available; skipping"
  fi

  if greentic-i18n-translator status --help >/dev/null 2>&1; then
    log "running translator status"
    run_status_per_locale || fail "translator status failed"
  else
    log "warning: translator status command not available; skipping"
  fi
}

[[ -f "$LOCALES_FILE" ]] || fail "missing locales file: $LOCALES_FILE"
[[ -f "$SOURCE_FILE" ]] || fail "missing source locale file: $SOURCE_FILE"

ensure_codex
ensure_codex_login
probe_translator
run_translate
run_optional_checks

log "translations updated. Run cargo build to embed translations into WASM"
