#!/usr/bin/env bash
# bootstrap.sh — Echo6 Vault Engine setup
#
# Idempotent setup script. Safe to re-run; each step is guarded.
# Run from any directory. Script locates itself via SCRIPT_DIR.
#
# Steps:
#   1. Verify services reachable (ollama, TEI, qdrant)
#   2. [DISABLED] Pull base model (~8 GB) — enable when ready
#   3. [DISABLED] Build vault-tagger modelfile — depends on step 2
#   4. TODO: Install Python deps for lib/
#   5. TODO: Generate initial vocab (lib/vocab_gen.py)
#   6. TODO: Install git pre-commit hook
#   7. TODO: Install cron job from config.yaml schedule

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG="${SCRIPT_DIR}/config.yaml"
MODELFILE="${SCRIPT_DIR}/Modelfile"

echo "==> Echo6 Vault Engine bootstrap"
echo "    engine_dir : ${SCRIPT_DIR}"
echo "    config     : ${CONFIG}"
echo ""

# ---------------------------------------------------------------------------
# Step 1 — Verify services reachable
# ---------------------------------------------------------------------------
echo "[1/7] Checking service endpoints..."

check_endpoint() {
    local name="$1"
    local url="$2"
    if curl -sf --max-time 5 "$url" > /dev/null 2>&1; then
        echo "      OK  ${name} (${url})"
    else
        echo "      FAIL ${name} (${url}) — is the service running?"
        return 1
    fi
}

check_endpoint "ollama"  "http://localhost:11434"
check_endpoint "TEI/bge-m3" "http://localhost:8090/health"
check_endpoint "qdrant"  "http://localhost:6333"

echo ""

# ---------------------------------------------------------------------------
# Step 2 — Pull base model (DISABLED — enable when ready to pull ~8 GB)
# ---------------------------------------------------------------------------
# Uncomment the following block together with Step 3 when ready:
#
# echo "[2/7] Pulling base model qwen2.5:7b-instruct-q8_0..."
# ollama pull qwen2.5:7b-instruct-q8_0
# echo ""

echo "[2/7] SKIPPED — base model pull disabled. Uncomment in bootstrap.sh when ready."
echo ""

# ---------------------------------------------------------------------------
# Step 3 — Build vault-tagger from Modelfile (DISABLED — depends on step 2)
# ---------------------------------------------------------------------------
# Uncomment together with Step 2:
#
# echo "[3/7] Building vault-tagger model from Modelfile..."
# ollama create vault-tagger -f "${MODELFILE}"
# echo ""

echo "[3/7] SKIPPED — vault-tagger build disabled. Uncomment after pulling base model."
echo ""

# ---------------------------------------------------------------------------
# Step 4 — Python dependencies for lib/
# ---------------------------------------------------------------------------
# TODO (Step 2): Install Python deps once requirements.txt is written.
# DO NOT run pip install without explicit approval from Matt.
#
# Example (do NOT uncomment without approval):
#   pip install -r "${SCRIPT_DIR}/requirements.txt"

echo "[4/7] TODO — Python deps not yet defined. See lib/ stubs. Do not pip install without approval."
echo ""

# ---------------------------------------------------------------------------
# Step 5 — Generate initial vocab (entity lexicon)
# ---------------------------------------------------------------------------
# TODO (Step 3): Calls lib/vocab_gen.py to query proxmox/docker/headscale
# and write engine/vocab.json.
#
# Example:
#   python3 "${SCRIPT_DIR}/lib/vocab_gen.py"

echo "[5/7] TODO — vocab_gen.py not yet implemented (Step 3)."
echo ""

# ---------------------------------------------------------------------------
# Step 6 — Install git pre-commit hook
# ---------------------------------------------------------------------------
# TODO (Step 4): Symlink or copy a pre-commit hook that runs lint.py
# against staged vault docs before commit.
#
# Example:
#   HOOK="${SCRIPT_DIR}/../../.git/hooks/pre-commit"
#   ln -sf "${SCRIPT_DIR}/hooks/pre-commit" "${HOOK}"

echo "[6/7] TODO — git pre-commit hook not yet implemented (Step 4)."
echo ""

# ---------------------------------------------------------------------------
# Step 7 — Install cron job
# ---------------------------------------------------------------------------
# TODO (Step 6): Install cron from schedule.cron in config.yaml.
# Cron entry should call sweep.sh with appropriate guards.
#
# config.yaml schedule.cron: "0 9 * * *"
# Example crontab line:
#   0 9 * * * /home/zvx/projects/.ref/engine/sweep.sh >> /home/zvx/projects/.ref/engine/sweep.log 2>&1

echo "[7/7] TODO — cron job not yet installed (Step 6)."
echo ""

echo "==> bootstrap.sh complete (partial — disabled steps noted above)."
