#!/bin/bash
# Healthcheck for ollama-piha (local CPU embed fallback for kb-query)

# Container must be running
if ! docker ps --filter "name=ollama-piha" --filter "status=running" | grep -qw "ollama-piha"; then
    echo "[FAIL] ollama-piha container is not running"
    exit 1
fi

# API must answer on loopback
TAGS=$(curl -sf http://127.0.0.1:11434/api/tags)
if [ $? -ne 0 ]; then
    echo "[FAIL] ollama-piha API is not responding on 127.0.0.1:11434"
    exit 1
fi

# bge-m3 must be pulled — without it the API answers /api/tags but every kb-query
# fallback embed fails the model check (deploy step 3 in README.md).
if ! echo "$TAGS" | grep -q '"bge-m3'; then
    echo "[FAIL] model bge-m3 missing (run: docker exec ollama-piha ollama pull bge-m3)"
    exit 1
fi

echo "[OK] ollama-piha is healthy (bge-m3 present)"
exit 0
