#!/bin/bash
# Post-deploy verification for DocFast
set -e

echo "⏳ Waiting for container to be healthy..."
for i in $(seq 1 30); do
  STATUS=$(docker inspect --format='{{.State.Health.Status}}' docfast-docfast-1 2>/dev/null || echo "not found")
  if [ "$STATUS" = "healthy" ]; then
    echo "✅ Container healthy"
    break
  fi
  if [ $i -eq 30 ]; then
    echo "❌ Container not healthy after 30s"
    exit 1
  fi
  sleep 1
done

echo "🔍 Checking health endpoint..."
HEALTH=$(curl -sf http://localhost:3100/health | python3 -c "import sys,json; d=json.load(sys.stdin); print(d['status'])" 2>/dev/null)
if [ "$HEALTH" != "ok" ]; then
  echo "❌ Health check failed"
  exit 1
fi
echo "✅ Health OK"

echo "🔍 Checking Stripe checkout..."
CHECKOUT=$(curl -sf -X POST http://localhost:3100/v1/billing/checkout -H "Content-Type: application/json" 2>&1)
if echo "$CHECKOUT" | grep -q '"url"'; then
  echo "✅ Stripe checkout working"
elif echo "$CHECKOUT" | grep -q 'STRIPE_SECRET_KEY'; then
  echo "❌ STRIPE_SECRET_KEY not configured!"
  exit 1
else
  echo "⚠️ Checkout returned: $CHECKOUT"
fi

echo ""
echo "🎉 Deploy verification passed!"
