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

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
ENV_FILE="${ENV_FILE:-$ROOT_DIR/.env.example}"
BACKEND_BASE_URL="${BACKEND_BASE_URL:-http://127.0.0.1:18090}"
PADDLE_HEALTH_URL="${PADDLE_HEALTH_URL:-http://192.168.1.13:18080/health}"
PP_CHAT_OCR_HEALTH_URL="${PP_CHAT_OCR_HEALTH_URL:-http://192.168.1.13:18083/health}"

echo "== OCR Platform production smoke check =="
echo "env file: $ENV_FILE"

echo
echo "== Compose config =="
docker compose --env-file "$ENV_FILE" -f "$ROOT_DIR/deploy/docker-compose.prod.yml" config >/tmp/ocr-platform-compose-prod.yml
grep -E "OCR_ENGINE_DEFAULT_CODE|PADDLE_LAYOUT_ENABLED|PADDLE_LAYOUT_ENDPOINT|SPRING_DATASOURCE_URL" /tmp/ocr-platform-compose-prod.yml

echo
echo "== Algorithm health =="
curl -fsS --max-time 10 "$PADDLE_HEALTH_URL"
echo
curl -fsS --max-time 10 "$PP_CHAT_OCR_HEALTH_URL"
echo

echo
echo "== Backend health (optional, skipped if backend is not running) =="
if curl -fsS --max-time 5 "$BACKEND_BASE_URL/actuator/health"; then
  echo
  echo "Backend is reachable."
  echo
  echo "== Engine registry =="
  curl -fsS --max-time 5 "$BACKEND_BASE_URL/api/v1/engines"
  echo
else
  echo "Backend is not reachable at $BACKEND_BASE_URL; deploy backend before full acceptance."
fi
