#!/usr/bin/env bash
# deploy/healthcheck.sh
# Verifies that the portal is running and responding.
#
# Usage:
#   bash deploy/healthcheck.sh

PORT="${PORT:-3002}"
BASE_PATH="${NEXT_PUBLIC_BASE_PATH:-/ytwatch}"
URL="http://localhost:${PORT}${BASE_PATH}/api/auth/me"

echo "==> Health check: ${URL}"

HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "${URL}")

if [ "$HTTP_STATUS" = "200" ] || [ "$HTTP_STATUS" = "401" ] || [ "$HTTP_STATUS" = "403" ]; then
  # 200 = authenticated, 401/403 = portal is running but not logged in — both are healthy
  echo "    Portal is running. HTTP status: ${HTTP_STATUS}"
  exit 0
else
  echo "ERROR: Portal did not respond correctly. HTTP status: ${HTTP_STATUS}"
  echo "       Check PM2 logs:  pm2 logs ytwatch --lines 50"
  exit 1
fi
