#!/usr/bin/env bash
# ==================================================
# deploy-podman.sh -- local podman deployment
# ==================================================
set -euo pipefail

PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
IMAGE_NAME="marker-api"
IMAGE_TAG="latest"
PORT="${PORT:-8000}"

ENV_FILE="${PROJECT_DIR}/.env"

echo "========================================="
echo "  marker-api  --  Podman deployment"
echo "========================================="
echo ""

# ---- Load env ----
if [[ -f "$ENV_FILE" ]]; then
    echo "Loading .env from ${ENV_FILE}"
    set -a; source "$ENV_FILE"; set +a
else
    echo "No .env found, using defaults"
fi

# ---- Build ----
echo ""
echo ">> Building image ${IMAGE_NAME}:${IMAGE_TAG} ..."
podman build -t "${IMAGE_NAME}:${IMAGE_TAG}" -f "${PROJECT_DIR}/Containerfile" "${PROJECT_DIR}"

# ---- Stop previous container ----
if podman ps --format '{{.Names}}' | grep -q "^${IMAGE_NAME}$"; then
    echo ">> Stopping existing container '${IMAGE_NAME}' ..."
    podman stop "${IMAGE_NAME}"
    podman rm "${IMAGE_NAME}"
fi

# ---- Run ----
echo ""
echo ">> Starting container '${IMAGE_NAME}' on port ${PORT} ..."
podman run -d \
    --name "${IMAGE_NAME}" \
    --network host \
    -p "${PORT}:8000" \
    -v /dev/kfd:/dev/kfd \
    -v /dev/dri:/dev/dri \
    -e OLLAMA_HOST="${OLLAMA_HOST:-http://10.0.1.127:11434}" \
    -e DEESEEK_OCR_MODEL="${DEESEEK_OCR_MODEL:-deepseek-ocr}" \
    -e AMD_COMPUTE="${AMD_COMPUTE:-true}" \
    -e TORCH_DEVICE="${TORCH_DEVICE:-rocm}" \
    -e MODEL_DTYPE="${MODEL_DTYPE:-float32}" \
    -e PORT=8000 \
    -e HOST=0.0.0.0 \
    -e GUNICORN_WORKERS="${GUNICORN_WORKERS:-2}" \
    -e GUNICORN_THREADS="${GUNICORN_THREADS:-4}" \
    -e GUNICORN_TIMEOUT="${GUNICORN_TIMEOUT:-300}" \
    -v "${PROJECT_DIR}/conversion_results:/app/conversion_results:rw" \
    --restart unless-stopped \
    "${IMAGE_NAME}:${IMAGE_TAG}"

echo ""
echo ">> Waiting 60s for models to load ..."
sleep 60

# ---- Health check ----
if curl -sf http://localhost:${PORT}/health | grep -q '"status"'; then
    echo "✅ marker-api is UP and healthy!"
    echo "   Swagger UI:  http://localhost:${PORT}/docs"
    echo "   API docs:    http://localhost:${PORT}/marker"
    echo "   Health:      http://localhost:${PORT}/health"
else
    echo "⚠️  Container running but health check may fail during model loading."
    echo "   Check logs:  podman logs -f ${IMAGE_NAME}"
    echo "   Container:   podman ps | grep ${IMAGE_NAME}"
fi
