#!/bin/bash

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
ENV_FILE="${PROJECT_DIR}/.env.development"

# Load env vars (silently)
source "$ENV_FILE" 2>/dev/null || true

# Path defaults (can be overridden by env vars above)
LOG_DIR="${MOMENTRY_LOG_DIR:-/Users/accusys/momentry/logs}"
PG_BIN_DIR="${MOMENTRY_PG_BIN_DIR:-/Users/accusys/pgsql/18.3/bin}"
PG_DATA_DIR="${MOMENTRY_PG_DATA_DIR:-/Users/accusys/pgsql/data}"
QDRANT_BIN="${MOMENTRY_QDRANT_BIN:-/Users/accusys/.cargo/bin/qdrant}"
QDRANT_STORAGE_DIR="${MOMENTRY_QDRANT_STORAGE_DIR:-/Users/accusys/momentry/qdrant_storage}"
LLAMACPP_BIN="${MOMENTRY_LLAMACPP_BIN:-/Users/accusys/llama/bin/llama-server}"
A4B_MODEL="${MOMENTRY_LLM_A4B_MODEL_PATH:-/Users/accusys/models/google_gemma-4-26B-A4B-it-Q5_K_M.gguf}"
A4B_MMPROJ="${MOMENTRY_LLM_A4B_MMPROJ_PATH:-/Users/accusys/models/gemma-4-26B-A4B-it.mmproj-f16.gguf}"
E4B_MODEL="${MOMENTRY_LLM_E4B_MODEL_PATH:-/Users/accusys/models/gemma-4-E4B-it-Q4_K_M.gguf}"
E4B_MMPROJ="${MOMENTRY_LLM_E4B_MMPROJ_PATH:-/Users/accusys/models/mmproj-gemma-4-E4B-it-BF16.gguf}"
OLLAMA_BIN="${MOMENTRY_OLLAMA_BIN:-/Users/accusys/bin/ollama}"
PLAYGROUND_BIN="${MOMENTRY_PLAYGROUND_BIN:-target/debug/momentry_playground}"
API_KEY="${MOMENTRY_API_KEY:-muser_68600856036340bcafc01930eb4bd839_1774418104_97221b69}"

# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m'

FAILURES=()

check() {
    if [ $? -eq 0 ]; then echo -e "  ${GREEN}✅${NC} $1"; else echo -e "  ${RED}❌${NC} $1"; FAILURES+=("$1"); fi
}

echo -e "${CYAN}========================================${NC}"
echo -e "${CYAN}  Momentry Core - Startup Sequence${NC}"
echo -e "${CYAN}========================================${NC}"
echo ""

# ── 1. PostgreSQL ──
echo -e "${YELLOW}[1/10] PostgreSQL${NC}"
if "$PG_BIN_DIR/pg_isready" -q 2>/dev/null; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    "$PG_BIN_DIR/pg_ctl" -D "$PG_DATA_DIR" -l "$LOG_DIR/pg.log" start 2>/dev/null
    sleep 2
    "$PG_BIN_DIR/pg_isready" -q 2>/dev/null; check "started"
fi

# ── 2. Redis ──
echo -e "${YELLOW}[2/10] Redis${NC}"
if redis-cli ping 2>/dev/null | grep -q PONG; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    brew services start redis 2>/dev/null || redis-server --daemonize yes 2>/dev/null
    sleep 2
    redis-cli ping 2>/dev/null | grep -q PONG; check "started"
fi

# ── 3. MongoDB ──
echo -e "${YELLOW}[3/10] MongoDB${NC}"
if pgrep -q mongod 2>/dev/null; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    brew services start mongodb-community 2>/dev/null || mongod --dbpath /opt/homebrew/var/mongodb --logpath "$LOG_DIR/mongodb.log" --fork 2>/dev/null
    sleep 2
    pgrep -q mongod 2>/dev/null; check "started"
fi

# ── 4. Qdrant ──
echo -e "${YELLOW}[4/10] Qdrant${NC}"
if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 http://localhost:6333/healthz 2>/dev/null | grep -q 200; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    mkdir -p "$QDRANT_STORAGE_DIR"
    "$QDRANT_BIN" > "$LOG_DIR/qdrant.log" 2>&1 &
    for i in $(seq 1 15); do
        sleep 2
        if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 2 http://localhost:6333/healthz 2>/dev/null | grep -q 200; then
            break
        fi
    done
    curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 http://localhost:6333/healthz 2>/dev/null | grep -q 200; check "started"
fi

# ── 5. Qdrant Collection ──
echo -e "${YELLOW}[5/10] Qdrant Collection${NC}"
COLLECTION="${QDRANT_COLLECTION:-momentry_dev_rule1_v2}"
EXISTS=$(curl -s "http://localhost:6333/collections/$COLLECTION" 2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('result',{}).get('status','not_found'))" 2>/dev/null)
if [ "$EXISTS" = "not_found" ]; then
    curl -s -X PUT "http://localhost:6333/collections/$COLLECTION" \
        -H "Content-Type: application/json" \
        -d '{"vectors":{"size":768,"distance":"Cosine"}}' > /dev/null 2>&1
    sleep 1
fi
curl -s "http://localhost:6333/collections/$COLLECTION" 2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); s=d.get('result',{}).get('status','not_found'); assert s in ('green','ok'), f'unexpected status: {s}'" 2>/dev/null
check "collection '$COLLECTION' ready"

# ── 6a. LLM Chat (A4B, port 8082) ──
echo -e "${YELLOW}[6a/10] LLM Chat - A4B (port 8082)${NC}"
if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:8082/health 2>/dev/null | grep -q 200; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    LLAMA_ARGS_A4B=(
        -m "$A4B_MODEL"
        --mmproj "$A4B_MMPROJ"
        --host 0.0.0.0 --port 8082
        -ngl 99 -c 16384 --temp 0.1 --mlock --reasoning off
    )
    "$LLAMACPP_BIN" "${LLAMA_ARGS_A4B[@]}" > "$LOG_DIR/llama_a4b.log" 2>&1 &
    echo -e "  ${YELLOW}⏳ loading A4B model (~30s)...${NC}"
    for i in $(seq 1 30); do
        sleep 2
        if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 2 http://localhost:8082/health 2>/dev/null | grep -q 200; then
            break
        fi
    done
    curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 http://localhost:8082/health 2>/dev/null | grep -q 200; check "started"
fi

# ── 6b. LLM Vision (E4B, port 8083) ──
echo -e "${YELLOW}[6b/10] LLM Vision - E4B (port 8083)${NC}"
if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:8083/health 2>/dev/null | grep -q 200; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    LLAMA_ARGS_E4B=(
        -m "$E4B_MODEL"
        --mmproj "$E4B_MMPROJ"
        --host 0.0.0.0 --port 8083
        -ngl 99 -c 16384 --temp 0.1 --mlock
    )
    "$LLAMACPP_BIN" "${LLAMA_ARGS_E4B[@]}" > "$LOG_DIR/llama_e4b.log" 2>&1 &
    echo -e "  ${YELLOW}⏳ loading E4B model (~30s)...${NC}"
    for i in $(seq 1 30); do
        sleep 2
        if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 2 http://localhost:8083/health 2>/dev/null | grep -q 200; then
            break
        fi
    done
    curl -s -o /dev/null -w "%{http_code}" --connect-timeout 3 http://localhost:8083/health 2>/dev/null | grep -q 200; check "started"
fi

# ── 7. Embedding Server ──
echo -e "${YELLOW}[7/10] EmbeddingGemma${NC}"
if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:11436/health 2>/dev/null | grep -q 200; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    VENV_PYTHON="${PROJECT_DIR}/venv/bin/python"
    EMBED_SCRIPT="${PROJECT_DIR}/scripts/embeddinggemma_server.py"
    if [ ! -f "$VENV_PYTHON" ]; then
        VENV_PYTHON="/opt/homebrew/bin/python3.11"
        pip install flask 2>/dev/null || true
    fi
    "$VENV_PYTHON" "$EMBED_SCRIPT" --port 11436 > "$LOG_DIR/embed.log" 2>&1 &
    sleep 5
    curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:11436/health 2>/dev/null | grep -q 200; check "started"
fi

# ── 8. Playground Server ──
echo -e "${YELLOW}[8/10] Playground API Server${NC}"
if curl -s -o /dev/null -w "%{http_code}" -H "X-API-Key: $API_KEY" --connect-timeout 5 http://127.0.0.1:3003/api/v1/agents/5w1h/status 2>/dev/null | grep -q 200; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    cd "$PROJECT_DIR"
    $PLAYGROUND_BIN server > "$LOG_DIR/playground.log" 2>&1 &
    sleep 4
    curl -s -o /dev/null -w "%{http_code}" -H "X-API-Key: $API_KEY" --connect-timeout 5 http://127.0.0.1:3003/api/v1/agents/5w1h/status 2>/dev/null | grep -q 200; check "started"
fi

# ── 9. Ollama ──
echo -e "${YELLOW}[9/10] Ollama${NC}"
if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:11434/api/tags 2>/dev/null | grep -q 200; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    if [ ! -f "$OLLAMA_BIN" ]; then
        echo -e "  ${YELLOW}⚠ ollama binary not found, skipping${NC}"
    else
        "$OLLAMA_BIN" serve > "$LOG_DIR/ollama.log" 2>&1 &
        sleep 3
        curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:11434/api/tags 2>/dev/null | grep -q 200; check "started"
    fi
fi

# ── 10. SFTPGo ──
echo -e "${YELLOW}[10/10] SFTPGo${NC}"
if curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:8080/api/v1/version 2>/dev/null | grep -q 200; then
    echo -e "  ${GREEN}✅${NC} already running"
else
    /Users/accusys/bin/sftpgo serve -c /Users/accusys/momentry/etc/sftpgo > "$LOG_DIR/sftpgo.log" 2>&1 &
    sleep 3
    curl -s -o /dev/null -w "%{http_code}" --connect-timeout 5 http://localhost:8080/api/v1/version 2>/dev/null | grep -q 200; check "started"
fi

echo ""
if [ ${#FAILURES[@]} -eq 0 ]; then
    echo -e "${GREEN}====================================${NC}"
    echo -e "${GREEN}  All services running${NC}"
    echo -e "${GREEN}====================================${NC}"
else
    echo -e "${RED}====================================${NC}"
    echo -e "${RED}  ${#FAILURES[@]} service(s) failed to start${NC}"
    echo -e "${RED}====================================${NC}"
    for f in "${FAILURES[@]}"; do echo -e "  ${RED}❌${NC} $f"; done
fi
echo ""
echo "  Playground:   http://127.0.0.1:3003"
echo "  LLM Chat:     http://127.0.0.1:8082"
echo "  LLM Vision:   http://127.0.0.1:8083"
echo "  Embedding:    http://127.0.0.1:11436"
echo "  Ollama:       http://localhost:11434"
echo "  Qdrant:       http://localhost:6333"
echo "  PostgreSQL:   localhost:5432"
echo "  Redis:        localhost:6379"
echo "  MongoDB:      localhost:27017"
echo "  SFTPGo:       http://localhost:8080 (SFTP: port 2022)"
echo ""
