#!/bin/bash
# Fleet deployment script - runs on Alpha, deploys to Bravo/Charlie/Delta/Echo
# Uses WireGuard IPs for inter-node communication
# set -e removed: SSH/SCP may emit warnings that are non-fatal

NODES=("bravo:10.69.69.3" "charlie:10.69.69.4" "delta:10.69.69.5" "echo:10.69.69.6")
SSH_KEY="/home/automation/.ssh/automation_key"
SSH_OPTS="-i $SSH_KEY -p 30222 -o StrictHostKeyChecking=yes -o ConnectTimeout=10"
STAGING="/tmp/fleet_deploy"

echo "============================================"
echo " Fleet Deployment - v2.3 Canonical Agent"
echo "============================================"

# 1. Stage files
echo ""
echo "[1/5] Staging deployment package..."
rm -rf "$STAGING"
mkdir -p "$STAGING"

# Copy canonical agent + dispatcher from Alpha's live install
cp /opt/binary/tools/python/s3rv1c3agent.py "$STAGING/"
cp /opt/binary/tools/python/self_heal_dispatcher.py "$STAGING/"

# Create systemd units
cat > "$STAGING/s3rv1c3agent.service" << 'UNIT'
[Unit]
Description=s3rv1c3agent per-node tooling and telemetry bridge
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=automation
Group=automation
EnvironmentFile=/etc/binary/agent.env
ExecStart=/usr/bin/python3 /opt/binary/tools/python/s3rv1c3agent.py
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
UNIT

cat > "$STAGING/self_heal_dispatcher.service" << 'UNIT'
[Unit]
Description=Self-healing dispatcher queue processor
After=network-online.target mariadb.service
Wants=network-online.target

[Service]
Type=simple
User=automation
Group=automation
EnvironmentFile=/etc/binary/tooling.env
ExecStart=/usr/bin/python3 /opt/binary/tools/python/self_heal_dispatcher.py --interval 5
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
UNIT

# Copy HMAC key from Alpha
cp /etc/binary/hmac.key "$STAGING/hmac.key" 2>/dev/null || echo "no-hmac-key" > "$STAGING/hmac.key"

# Copy tooling.env template from Alpha
cp /etc/binary/tooling.env "$STAGING/tooling.env" 2>/dev/null || true

echo "  Staged: s3rv1c3agent.py, self_heal_dispatcher.py, systemd units, configs"

# 2. Check current state of each node
echo ""
echo "[2/5] Checking current state of fleet nodes..."
for entry in "${NODES[@]}"; do
    IFS=':' read -r name ip <<< "$entry"
    echo -n "  $name ($ip): "
    status=$(curl -sf --connect-timeout 3 "http://$ip:8366/health" 2>/dev/null | python3 -c 'import sys,json; d=json.load(sys.stdin); print(d.get("version","?"))' 2>/dev/null || echo "no-agent")
    echo "$status"
done

# 3. Deploy to each node
echo ""
echo "[3/5] Deploying to fleet nodes..."
for entry in "${NODES[@]}"; do
    IFS=':' read -r name ip <<< "$entry"
    echo ""
    echo "  === $name ($ip) ==="
    
    # Stop old agent
    echo "  [a] Stopping old services..."
    ssh $SSH_OPTS automation@$ip "sudo systemctl stop s3rv1c3agent 2>/dev/null; sudo systemctl stop self_heal_dispatcher 2>/dev/null; sudo systemctl disable s3rv1c3agent 2>/dev/null; sudo systemctl disable self_heal_dispatcher 2>/dev/null; echo '    Stopped'" 2>/dev/null || echo "    Warning: could not stop services"
    
    # Create directories
    echo "  [b] Creating directories..."
    ssh $SSH_OPTS automation@$ip "sudo mkdir -p /opt/binary/tools/python /opt/binary/tools/bash /etc/binary /var/log/svc /var/spool/s3rv1c3agent && sudo chown -R automation:automation /opt/binary/tools /var/log/svc /var/spool/s3rv1c3agent && echo '    Created'" 2>/dev/null
    
    # Upload files
    echo "  [c] Uploading canonical files..."
    scp $SSH_OPTS "$STAGING/s3rv1c3agent.py" automation@$ip:/tmp/s3rv1c3agent.py 2>/dev/null
    scp $SSH_OPTS "$STAGING/self_heal_dispatcher.py" automation@$ip:/tmp/self_heal_dispatcher.py 2>/dev/null
    scp $SSH_OPTS "$STAGING/s3rv1c3agent.service" automation@$ip:/tmp/s3rv1c3agent.service 2>/dev/null
    scp $SSH_OPTS "$STAGING/self_heal_dispatcher.service" automation@$ip:/tmp/self_heal_dispatcher.service 2>/dev/null
    scp $SSH_OPTS "$STAGING/hmac.key" automation@$ip:/tmp/hmac.key 2>/dev/null
    
    # Install files
    echo "  [d] Installing..."
    ssh $SSH_OPTS automation@$ip "
        sudo cp /tmp/s3rv1c3agent.py /opt/binary/tools/python/s3rv1c3agent.py
        sudo cp /tmp/self_heal_dispatcher.py /opt/binary/tools/python/self_heal_dispatcher.py
        sudo cp /tmp/s3rv1c3agent.service /etc/systemd/system/s3rv1c3agent.service
        sudo cp /tmp/self_heal_dispatcher.service /etc/systemd/system/self_heal_dispatcher.service
        sudo cp /tmp/hmac.key /etc/binary/hmac.key
        sudo chown automation:automation /opt/binary/tools/python/*.py
        sudo chmod 755 /opt/binary/tools/python/*.py
        sudo chown automation:automation /etc/binary/hmac.key
        sudo chmod 640 /etc/binary/hmac.key
        sudo rm -f /tmp/s3rv1c3agent.py /tmp/self_heal_dispatcher.py /tmp/s3rv1c3agent.service /tmp/self_heal_dispatcher.service /tmp/hmac.key
        echo '    Installed'
    " 2>/dev/null
    
    # Create node-specific agent.env
    echo "  [e] Writing agent.env for $name..."
    ssh $SSH_OPTS automation@$ip "
        sudo tee /etc/binary/agent.env > /dev/null << EOF
NODE_ID=$name
AGENT_PORT=8366
AGENT_BIND=0.0.0.0
HMAC_KEY_FILE=/etc/binary/hmac.key
ALLOWED_SOURCES=10.69.69.1,10.69.69.3,10.69.69.4,10.69.69.5,10.69.69.6,127.0.0.1
LOG_DIR=/var/log/svc
SPOOL_DIR=/var/spool/s3rv1c3agent
DB_PUSH_URL=
CTL_PATH=/opt/binary/tools/bash/s3rv1c3agent.sh
VEIL_PATH=/opt/binary/tools/bash/veil.sh
EOF
        sudo chown automation:automation /etc/binary/agent.env
        sudo chmod 640 /etc/binary/agent.env
        echo '    agent.env written'
    " 2>/dev/null
    
    # Enable and start
    echo "  [f] Starting services..."
    ssh $SSH_OPTS automation@$ip "
        sudo systemctl daemon-reload
        sudo systemctl enable s3rv1c3agent
        sudo systemctl start s3rv1c3agent
        sleep 2
        echo '    Status:'
        sudo systemctl is-active s3rv1c3agent 2>/dev/null || true
    " 2>/dev/null
done

# 4. Verify
echo ""
echo "[4/5] Verifying fleet connectivity..."
sleep 3
for entry in "${NODES[@]}"; do
    IFS=':' read -r name ip <<< "$entry"
    echo -n "  $name ($ip): "
    result=$(curl -sf --connect-timeout 5 "http://$ip:8366/health" 2>/dev/null | python3 -c 'import sys,json; d=json.load(sys.stdin); print(f"v{d[\"version\"]} node={d[\"node\"]} status={d[\"status\"]}")' 2>/dev/null || echo "FAILED")
    echo "$result"
done

# Also verify Alpha
echo -n "  alpha (10.69.69.1): "
result=$(curl -sf "http://10.69.69.1:8366/health" | python3 -c 'import sys,json; d=json.load(sys.stdin); print(f"v{d[\"version\"]} node={d[\"node\"]} status={d[\"status\"]}")' 2>/dev/null || echo "FAILED")
echo "$result"

# 5. Cleanup
echo ""
echo "[5/5] Cleaning up staging..."
rm -rf "$STAGING"

echo ""
echo "============================================"
echo " Fleet deployment complete"
echo "============================================"
