#!/bin/bash
# Memory Stack Launch Script for NODA2
# Hybrid Mode: Local Memory with optional NODA1 access

set -e

REPO_DIR="/Users/apple/github-projects/microdao-daarion"
COMPOSE_FILE="$REPO_DIR/docker-compose.memory-node2.yml"

echo "🧠 DAARION Memory Stack - NODA2"
echo "================================"
echo ""

# Check .env file
if [ ! -f "$REPO_DIR/.env" ]; then
    echo "❌ Error: .env file not found!"
    echo "   Creating .env file..."
    touch "$REPO_DIR/.env"
fi

# Check required API keys
if ! grep -q "COHERE_API_KEY" "$REPO_DIR/.env" 2>/dev/null; then
    echo "❌ Error: COHERE_API_KEY not found in .env"
    echo "   Please add: COHERE_API_KEY=your_key_here"
    exit 1
fi

echo "✅ Environment variables configured"
echo ""

# Create data directories
echo "📁 Creating data directories..."
mkdir -p "$REPO_DIR/data"/{qdrant-node2,postgres-node2,neo4j-node2,redis-node2}
echo "   Done!"
echo ""

# Pull latest images
echo "📦 Pulling Docker images..."
docker-compose -f "$COMPOSE_FILE" pull
echo ""

# Start services
echo "🚀 Starting Memory Stack..."
echo "   This may take a few minutes..."
echo ""

docker-compose -f "$COMPOSE_FILE" up -d

echo ""
echo "⏳ Waiting for services to be healthy..."
sleep 10

# Health checks
echo ""
echo "📊 Service Status:"
echo "=================="
echo ""

# Qdrant
echo "Qdrant (Vector DB):"
if curl -s -o /dev/null -w "   HTTP Status: %{http_code}\n" --connect-timeout 3 http://localhost:6333/healthz; then
    echo "   ✅ Healthy"
else
    echo "   ⏳ Starting..."
fi

# PostgreSQL
echo ""
echo "PostgreSQL (Relational DB):"
if docker exec dagi-postgres-node2 pg_isready -U daarion -d daarion_memory >/dev/null 2>&1; then
    echo "   ✅ Healthy"
else
    echo "   ⏳ Starting..."
fi

# Neo4j
echo ""
echo "Neo4j (Graph DB):"
if curl -s -o /dev/null -w "   HTTP Status: %{http_code}\n" --connect-timeout 3 http://localhost:7474 >/dev/null 2>&1; then
    echo "   ✅ Healthy"
else
    echo "   ⏳ Starting (may take 30-40 seconds)..."
fi

# Memory Service
echo ""
echo "Memory Service:"
if curl -s -o /dev/null -w "   HTTP Status: %{http_code}\n" --connect-timeout 3 http://localhost:8000/health >/dev/null 2>&1; then
    echo "   ✅ Healthy"
else
    echo "   ⏳ Starting..."
fi

# Redis
echo ""
echo "Redis (Cache):"
if docker exec dagi-redis-node2 redis-cli ping >/dev/null 2>&1; then
    echo "   ✅ Healthy"
else
    echo "   ⏳ Starting..."
fi

echo ""
echo "================================"
echo "✅ Memory Stack launched!"
echo ""
echo "📋 Endpoints:"
echo "   • Qdrant UI:         http://localhost:6333/dashboard"
echo "   • Memory Service:    http://localhost:8000"
echo "   • Memory Health:     http://localhost:8000/health"
echo "   • Neo4j Browser:     http://localhost:7474"
echo "   • Adminer (DB UI):   http://localhost:8080"
echo ""
echo "📝 Connection Strings:"
echo "   • Qdrant:       http://localhost:6333"
echo "   • PostgreSQL:   postgresql://daarion:daarion_secret_node2@localhost:5433/daarion_memory"
echo "   • Neo4j:        bolt://localhost:7687"
echo "   • Redis:        redis://localhost:6379"
echo ""
echo "🔧 Useful Commands:"
echo "   • View logs:        docker-compose -f $COMPOSE_FILE logs -f"
echo "   • Stop all:         docker-compose -f $COMPOSE_FILE down"
echo "   • Restart service:  docker-compose -f $COMPOSE_FILE restart memory-service-node2"
echo "   • Check status:     docker-compose -f $COMPOSE_FILE ps"
echo ""
echo "📚 Documentation:"
echo "   • Setup Guide:      docs/NODA2-MEMORY-SETUP.md"
echo "   • API Docs:         http://localhost:8000/docs (after launch)"
echo ""
echo "🎯 Next Steps:"
echo "   1. Wait 30-60 seconds for all services to start"
echo "   2. Check health: curl http://localhost:8000/health"
echo "   3. Initialize Sofiia collections (see docs)"
echo "   4. Configure OpenClaw to use memory service"
echo ""
