#!/bin/bash
# scripts/spawn_local_agent.sh

echo "🚀 Cortex Agent Node - Local Spawner"
echo "===================================="

# Check for environment variables
read -p "Enter Node ID (Default: local-agent-$(hostname)): " NODE_ID
NODE_ID=${NODE_ID:-local-agent-$(hostname)}

echo ""
echo "STEP 1: Register your node"
echo "--------------------------"
echo "Go to: http://127.0.0.1:8000/nodes"
echo "1. Click '+ Register Node'"
echo "2. Use ID: $NODE_ID"
echo "3. Copy the 'Secret Key' provided."
echo ""

read -sp "Enter the Secret Key: " SECRET_KEY
echo ""

read -p "Enter Hub gRPC Endpoint (Default: 127.0.0.1:50051): " GRPC_ENDPOINT
GRPC_ENDPOINT=${GRPC_ENDPOINT:-127.0.0.1:50051}

# Create .env file
cat > .env <<EOF
AGENT_NODE_ID=$NODE_ID
AGENT_SECRET_KEY=$SECRET_KEY
GRPC_ENDPOINT=$GRPC_ENDPOINT
AGENT_TLS_ENABLED=true
EOF

echo "✅ Configuration saved to .env"

# Build and Start
echo "📦 Building and starting container..."
docker compose build
docker compose up -d

echo ""
echo "🎉 Agent '$NODE_ID' is now running!"
echo "Check logs with: docker logs -f cortex-local-agent"
