#!/bin/bash
# Momentry v1.0.0 Release Pre-flight Check
# Checks all required services and models before deployment

set -e

PASS=0
FAIL=0
WARN=0

check() {
    local name="$1"
    local cmd="$2"
    if eval "$cmd" > /dev/null 2>&1; then
        echo "✓ $name"
        PASS=$((PASS + 1))
    else
        echo "✗ $name FAILED"
        FAIL=$((FAIL + 1))
    fi
}

warn() {
    local name="$1"
    echo "⚠ $name (non-critical)"
    WARN=$((WARN + 1))
}

echo "====================================="
echo "Momentry v1.0.0 Pre-flight Check"
echo "Date: $(date '+%Y-%m-%d %H:%M:%S')"
echo "====================================="
echo ""

# --- Core Services ---
echo "--- Core Services ---"

# PostgreSQL
check "PostgreSQL" "pg_isready -U accusys -h localhost"

# Redis
check "Redis" "redis-cli -a accusys PING"

# MongoDB
check "MongoDB" "mongosh --quiet --eval 'db.runCommand({ping:1})'"

# Qdrant (requires API key)
QDRANT_API_KEY="${QDRANT_API_KEY:-Test3200Test3200Test3200}"
check "Qdrant (port 6333)" "curl -sf -H 'api-key: $QDRANT_API_KEY' http://localhost:6333/collections"

echo ""

# --- Inference Engines ---
echo "--- Inference Engines ---"

# Ollama
if curl -sf http://localhost:11434/api/tags > /dev/null 2>&1; then
    check "Ollama (port 11434)" "true"
    # Check specific model
    if curl -sf http://localhost:11434/api/tags | grep -q "nomic-embed-text"; then
        check "  Model: nomic-embed-text" "true"
    else
        warn "  Model: nomic-embed-text not found"
    fi
else
    FAIL=$((FAIL + 2))
    echo "✗ Ollama (port 11433) FAILED"
    echo "✗   Model: nomic-embed-text (Ollama not running)"
fi

# llama-server
if curl -sf http://localhost:8081/v1/models > /dev/null 2>&1; then
    check "llama-server (port 8081)" "true"
    if curl -sf http://localhost:8081/v1/models | grep -q "gemma4"; then
        check "  Model: gemma4_e4b_q5" "true"
    else
        warn "  Model: gemma4_e4b_q5 not detected"
    fi
else
    FAIL=$((FAIL + 2))
    echo "✗ llama-server (port 8081) FAILED"
    echo "✗   Model: gemma4_e4b_q5 (llama-server not running)"
fi

echo ""

# --- External Tools ---
echo "--- External Tools ---"

check "ffmpeg" "command -v ffmpeg"
check "ffprobe" "command -v ffprobe"

PYTHON_PATH="${MOMENTRY_PYTHON_PATH:-/opt/homebrew/bin/python3.11}"
check "Python 3.11 ($PYTHON_PATH)" "test -f $PYTHON_PATH"

echo ""

# --- File Services ---
echo "--- File Services ---"

check "SFTPGo (port 8080)" "curl -sf http://localhost:8080/"

# Check demo user data directory
if [ -d "/Users/accusys/momentry/var/sftpgo/data/demo" ]; then
    check "SFTPGo demo data directory" "true"
else
    warn "SFTPGo demo data directory not found"
fi

echo ""

# --- Environment ---
echo "--- Environment ---"

# Check .env file for production
if [ -f ".env" ]; then
    check ".env file exists" "true"
else
    warn ".env file not found (using defaults)"
fi

# Check port 3002 availability
if lsof -i :3002 > /dev/null 2>&1; then
    warn "Port 3002 in use (existing production service running)"
else
    check "Port 3002 available" "true"
fi

# Check port 3003 availability
if lsof -i :3003 > /dev/null 2>&1; then
    warn "Port 3003 in use (playground running)"
else
    check "Port 3003 available" "true"
fi

echo ""

# --- Disk Space ---
echo "--- Disk Space ---"

DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$DISK_USAGE" -lt 90 ]; then
    check "Disk usage: ${DISK_USAGE}%" "true"
else
    FAIL=$((FAIL + 1))
    echo "✗ Disk usage: ${DISK_USAGE}% (CRITICAL - >90%)"
fi

echo ""
echo "====================================="
echo "Summary: $PASS passed, $FAIL failed, $WARN warnings"
echo "====================================="

if [ "$FAIL" -gt 0 ]; then
    echo ""
    echo "CRITICAL: $FAIL service(s) failed. Do NOT proceed with release."
    exit 1
else
    echo ""
    echo "All critical services ready. Safe to proceed with release."
    exit 0
fi
