#!/bin/bash
# =============================================================================
# CLI Automation Example for Graph_D
# =============================================================================
# This script demonstrates how to use graph_d CLI in automation scripts.
#
# Prerequisites:
#   cargo install graph_d --features cli
#
# Usage:
#   chmod +x examples/cli_automation.sh
#   ./examples/cli_automation.sh
# =============================================================================

set -e  # Exit on error

DB_FILE="automation_test.graphd"
GRAPH_D="graph_d"

# Clean up any existing test database
rm -f "$DB_FILE"

echo "=== Graph_D CLI Automation Demo ==="
echo ""

# -----------------------------------------------------------------------------
# 1. Create nodes using single commands
# -----------------------------------------------------------------------------
echo "1. Creating nodes..."

$GRAPH_D "$DB_FILE" -q -c "CREATE (n:Server {name: 'web-1', ip: '10.0.0.1', status: 'running'})"
$GRAPH_D "$DB_FILE" -q -c "CREATE (n:Server {name: 'web-2', ip: '10.0.0.2', status: 'running'})"
$GRAPH_D "$DB_FILE" -q -c "CREATE (n:Server {name: 'db-1', ip: '10.0.0.10', status: 'running'})"
$GRAPH_D "$DB_FILE" -q -c "CREATE (n:LoadBalancer {name: 'lb-1', ip: '10.0.0.100', status: 'running'})"

echo "   Created 4 infrastructure nodes"

# -----------------------------------------------------------------------------
# 2. Query and process results as JSON
# -----------------------------------------------------------------------------
echo ""
echo "2. Querying servers as JSON..."

SERVERS=$($GRAPH_D "$DB_FILE" -q -c "MATCH (n:Server) RETURN n.name, n.ip" -o json)
echo "$SERVERS" | head -20

# -----------------------------------------------------------------------------
# 3. Export data as CSV
# -----------------------------------------------------------------------------
echo ""
echo "3. Exporting to CSV..."

$GRAPH_D "$DB_FILE" -q -c "MATCH (n) RETURN n.name, n.status" -o csv > /tmp/graph_export.csv
echo "   Exported to /tmp/graph_export.csv"
cat /tmp/graph_export.csv

# -----------------------------------------------------------------------------
# 4. Run a script file
# -----------------------------------------------------------------------------
echo ""
echo "4. Running script file..."

# Create a temporary script
cat > /tmp/graph_queries.gql << 'EOF'
-- Count all nodes
MATCH (n) RETURN count(n);

-- Find running servers
MATCH (n:Server) WHERE n.status = 'running' RETURN n.name;
EOF

$GRAPH_D "$DB_FILE" -q -f /tmp/graph_queries.gql -o table

# -----------------------------------------------------------------------------
# 5. Conditional logic based on query results
# -----------------------------------------------------------------------------
echo ""
echo "5. Conditional logic example..."

COUNT=$($GRAPH_D "$DB_FILE" -q -c "MATCH (n:Server) RETURN count(n)" -o csv | tail -1)
echo "   Server count: $COUNT"

if [ "$COUNT" -ge 2 ]; then
    echo "   ✓ Sufficient servers for high availability"
else
    echo "   ✗ Warning: Need more servers for HA"
fi

# -----------------------------------------------------------------------------
# Cleanup
# -----------------------------------------------------------------------------
echo ""
echo "=== Demo Complete ==="
rm -f "$DB_FILE" /tmp/graph_queries.gql /tmp/graph_export.csv
echo "Cleaned up test files"
