#!/bin/bash
# Connect to your self-hosted n8n

echo "🔄 n8n Workflow Automation"
echo "=========================="
echo ""

# Check if n8n is accessible locally
if curl -s http://localhost:5678/healthz > /dev/null 2>&1; then
    echo "✅ Local n8n detected on localhost:5678"
    export N8N_HOST="http://localhost:5678"
elif curl -s http://n8n:5678/healthz > /dev/null 2>&1; then
    echo "✅ Docker n8n detected on n8n:5678"
    export N8N_HOST="http://n8n:5678"
else
    echo "⚠️  Couldn't auto-detect n8n. What's your n8n URL?"
    echo "   Examples: http://localhost:5678 or http://n8n.yourdomain.com"
    read -p "n8n URL: " N8N_HOST
    export N8N_HOST
fi

# Test connection
echo ""
echo "Testing connection to $N8N_HOST..."
if curl -s "$N8N_HOST/healthz" > /dev/null 2>&1; then
    echo "✅ Connected to n8n!"
    echo ""
    echo "What workflow do you want to build?"
    echo "Example ideas:"
    echo "  • 'When I star an email, create a Notion task'"
    echo "  • 'Daily morning briefing: check calendar + email + weather'"
    echo "  • 'When Home Assistant detects motion, send me a Telegram'"
    echo "  • 'Weekly report: aggregate data from multiple sources'"
else
    echo "❌ Could not connect to n8n"
    echo "Make sure your n8n instance is running and accessible"
fi
