#!/usr/bin/env bash
# Çok dilli vitrin deploy smoke (curl). Kullanım:
#   API_BASE=https://faunamix-production.up.railway.app ./scripts/verify-i18n-deploy.sh
set -euo pipefail

API_BASE="${API_BASE:-${NEXT_PUBLIC_API_URL:-http://localhost:8080}}"
API_BASE="${API_BASE%/}"

echo "==> API: $API_BASE"

nav_en="$(curl -fsS "${API_BASE}/api/nav_menu.php?locale=en" 2>/dev/null || true)"
if [[ -z "$nav_en" ]]; then
  echo "FAIL: nav_menu.php?locale=en yanıt yok"
  exit 1
fi
if echo "$nav_en" | grep -q '/iletisim'; then
  echo "FAIL: EN menüde /iletisim URL var — migration_i18n_nav_en_dedupe çalıştırın"
  exit 1
else
  echo "OK: EN menüde /iletisim yok (localized URL beklenir)"
fi

# Mükerrer URL (aynı path iki kez)
dup_count="$(echo "$nav_en" | python3 -c "
import json,sys
from collections import Counter
d=json.load(sys.stdin)
urls=[i.get('url','').strip() for i in d.get('items',[])]
c=Counter(urls)
dups=[u for u,n in c.items() if n>1 and u]
print(len(dups))
" 2>/dev/null || echo "0")"
if [[ "${dup_count:-0}" != "0" ]]; then
  echo "FAIL: EN menüde mükerrer URL var"
  exit 1
fi
echo "OK: EN menüde mükerrer kök URL yok"

nav_tr="$(curl -fsS "${API_BASE}/api/nav_menu.php" 2>/dev/null || true)"
tr_en_title=0
for en_label in '"title":"Home"' '"title":"Products"' '"title":"Contact"' '"title":"About"'; do
  if echo "$nav_tr" | grep -q "$en_label"; then
    tr_en_title=1
    break
  fi
done
if [[ "$tr_en_title" == "1" ]]; then
  echo "FAIL: TR menüde İngilizce kök başlık — panel Menü: TR title alanını Türkçe doldurun (EN → title_en)"
  exit 1
else
  echo "OK: TR menüde bariz EN kök başlık yok"
fi

page_en="$(curl -fsS "${API_BASE}/api/site_page.php?path=%2Fcontact&locale=en" 2>/dev/null || true)"
if echo "$page_en" | grep -q '"page":null'; then
  echo "OK: /contact EN strict — satır yok (404 vitrin)"
elif echo "$page_en" | grep -qi 'İletişim'; then
  echo "WARN: EN contact sayfasında Türkçe başlık — migration_i18n_en_empty_content veya panel EN metni"
else
  echo "OK: EN contact yanıtı var"
fi

page_about="$(curl -fsS "${API_BASE}/api/site_page.php?path=%2Fabout-us&locale=en" 2>/dev/null || true)"
if echo "$page_about" | grep -q '"page":null'; then
  echo "WARN: /about-us EN — path_en veya içerik eksik (migration_i18n_merge_locale_rows)"
else
  echo "OK: EN about-us yanıtı var (sütun modeli)"
fi

for pair in "distance-selling:en" "privacy:en" "returns:en"; do
  p="${pair%%:*}"
  loc="${pair##*:}"
  resolve="$(curl -fsS "${API_BASE}/api/resolve.php?path=%2F${p}&locale=${loc}" 2>/dev/null || true)"
  if echo "$resolve" | grep -q '"type":"page"'; then
    echo "OK: resolve /${p} locale=${loc} → page"
  else
    echo "WARN: resolve /${p} locale=${loc} — Rotaları senkronize et + migration_site_pages_seed_en_locale"
  fi
done

href_tr="$(curl -fsS "${API_BASE}/api/locale_href.php?path=%2Fcontact&from=en" 2>/dev/null || true)"
if echo "$href_tr" | grep -qE '"path":"(\\/|/)iletisim"'; then
  echo "OK: locale_href EN /contact → TR /iletisim (to= varsayılan)"
elif echo "$href_tr" | grep -q '"ok":true'; then
  echo "WARN: locale_href EN→TR yanıt var ama /iletisim değil — CMS path_en kontrol"
else
  echo "WARN: locale_href EN→TR başarısız — statik eşleme vitrinde yine çalışabilir"
fi

for legal_slug in privacy distance-selling returns terms; do
  legal_page="$(curl -fsS "${API_BASE}/api/site_page.php?path=%2F${legal_slug}&locale=en" 2>/dev/null || true)"
  if echo "$legal_page" | grep -q '"page":null'; then
    echo "WARN: /${legal_slug} EN site_page null — path_en veya yasal satır eksik"
  elif echo "$legal_page" | grep -q '"page_template":"legal"'; then
    echo "OK: site_page /${legal_slug} locale=en"
  else
    echo "OK: site_page /${legal_slug} locale=en (yanıt var)"
  fi
done

echo ""
echo "==> Panel / Vercel kontrol listesi (manuel):"
echo "  1) Vercel: NEXT_PUBLIC_I18N_LOCALES=tr,en → redeploy"
echo "  2) Panel: Genel ayarlar → Rotaları senkronize et"
echo "  3) Sayfalar: gizlilik, mesafeli satış, hakkımızda, iletişim — English sekmesi + path_en"
echo "  4) Menü EN: /contact, /about-us ( /iletisim değil )"
echo "  5) Vitrin smoke: /en/contact ↔ /iletisim, /en/listing, ürün breadcrumb"
echo ""
echo "==> Bitti. Vercel: NEXT_PUBLIC_I18N_LOCALES=tr,en ile redeploy şart."
