#!/bin/bash
# Deploy wuji-cp-0 control plane server on Hetzner
# Usage: ./deploy-cp-server.sh

set -euo pipefail

HCLOUD_TOKEN="${HCLOUD_TOKEN:?HCLOUD_TOKEN required}"
HOSTNAME="wuji-cp-0"
SERVER_TYPE="cax21"
IMAGE="120350"  # NixOS minimal aarch64
LOCATION="nbg1"
SSH_KEY="htz_ops"

echo "=== Deploying $HOSTNAME ==="
echo "Image: $IMAGE (NixOS minimal aarch64)"
echo "Type: $SERVER_TYPE | Location: $LOCATION"

# Create server
echo ""
echo "Creating server..."
hcloud server create \
  --name "$HOSTNAME" \
  --type "$SERVER_TYPE" \
  --location "$LOCATION" \
  --image "$IMAGE" \
  --ssh-key "$SSH_KEY" \
  --public-net enable_ipv4=true,ipv6=false

# Get server details
SERVER_ID=$(hcloud server describe "$HOSTNAME" -o format='{{.ID}}')
SERVER_IP=$(hcloud server describe "$HOSTNAME" -o format='{{.PublicNet.IPv4.IP}}')

echo ""
echo "✓ Server created!"
echo "  ID: $SERVER_ID"
echo "  IP: $SERVER_IP"
echo "  Hostname: $HOSTNAME"
echo ""
echo "Next steps:"
echo "1. Wait 30 seconds for SSH to become available"
echo "2. Connect: ssh -o StrictHostKeyChecking=no root@$SERVER_IP"
echo "3. Run provisioning bootstrap on the server"
echo ""
echo "SSH Key: $SSH_KEY"
echo "Get public IPs: hcloud server list"
echo "Delete: hcloud server delete $SERVER_ID"
