#!/usr/bin/env bash
# deploy-stability-agent.sh - Helper to deploy stability-agent (print or SSH)

NODE=$1
MODE="print"
[[ "$2" == "--ssh" ]] && MODE="ssh"

if [[ -z "$NODE" ]]; then
    echo "Usage: $0 <node-name> [--ssh]"
    echo "Supported nodes: chelsty, piha, solaria, vps"
    exit 1
fi

case "$NODE" in
    piha)    TARGET="100.108.208.3" ;;
    chelsty) TARGET="100.122.201.22" ;;
    vps)     TARGET="100.95.58.48" ;;
    solaria) TARGET="local" ;;
    *)
        echo "Error: Unknown node '$NODE'"
        echo "Supported nodes: chelsty, piha, solaria, vps"
        exit 1
        ;;
esac

echo "HOST: $NODE"
echo "MODE: $MODE"
echo "TARGET: $TARGET"

REPO_PATH="/home/oskar/homelab-codex-ws"

if [[ "$NODE" == "solaria" ]]; then
    if [[ "$MODE" == "ssh" ]]; then
        echo "--- Running local deployment for solaria ---"
        cd "$REPO_PATH" && git fetch origin && git checkout master && git pull origin master && cd services/stability-agent && ./deploy-local.sh solaria
    else
        echo "# --- Deployment commands for solaria ---"
        echo "cd $REPO_PATH"
        echo "git fetch origin"
        echo "git checkout master"
        echo "git pull origin master"
        echo "cd services/stability-agent"
        echo "./deploy-local.sh solaria"
    fi
else
    # Remote nodes
    SSH_CMD="ssh oskar@$TARGET 'cd $REPO_PATH && git fetch origin && git checkout master && git pull origin master && cd services/stability-agent && ./deploy-local.sh $NODE'"
    if [[ "$MODE" == "ssh" ]]; then
        echo "--- Deploying to $NODE ($TARGET) via SSH ---"
        eval "$SSH_CMD"
    else
        echo "# --- Deployment commands for $NODE ---"
        echo "$SSH_CMD"
    fi
fi
