#!/usr/bin/env bash
# Hızlı canlı smoke (API + vitrin kök). Secret yazdırmaz.
set -euo pipefail

API="${FAUNAMIX_SMOKE_API_URL:-https://faunamix-production.up.railway.app}"
SITE="${FAUNAMIX_SMOKE_SITE_URL:-https://www.faunamix.com}"

fail=0

echo "Faunamix production smoke"
echo "  api:  $API"
echo "  site: $SITE"
echo ""

if curl -sf "$API/health.php" | grep -q '"ok":true' && curl -sf "$API/health.php" | grep -q '"db":"ok"'; then
  echo "[OK] API health (ok + db)"
else
  echo "[FAIL] API health"
  fail=1
fi

if curl -sf -X POST "$API/api/filter.php" -H "Content-Type: application/json" -d '{"page":1,"page_size":1}' | grep -q '"products"'; then
  echo "[OK] filter products"
else
  echo "[FAIL] filter products"
  fail=1
fi

code=$(curl -sf -o /dev/null -w "%{http_code}" "$SITE/listing" || echo "000")
if [[ "$code" == "200" ]]; then
  echo "[OK] site listing HTTP $code"
else
  echo "[FAIL] site listing HTTP $code"
  fail=1
fi

echo ""
if [[ "$fail" -ne 0 ]]; then
  echo "Smoke FAIL"
  exit 1
fi
echo "Smoke PASS"
exit 0
