#!/bin/bash
# Deploy Uptime Kuma on main-pve
# LXC Container 128 at 10.0.10.26

set -e

echo "=== Uptime Kuma Deployment Script ==="
echo ""

# Configuration
VMID=128
HOSTNAME="uptime-kuma"
IP="10.0.10.26"
GATEWAY="10.0.10.1"
CORES=2
MEMORY=2048
SWAP=512
DISK="8"
TEMPLATE="local:vztmpl/debian-12-standard_12.7-1_amd64.tar.zst"

echo "Configuration:"
echo "  VMID: $VMID"
echo "  Hostname: $HOSTNAME"
echo "  IP: $IP/24"
echo "  Resources: ${CORES} cores, ${MEMORY}MB RAM, ${DISK}GB disk"
echo ""

# Check if container already exists
if ssh root@10.0.10.3 "pct status $VMID 2>/dev/null"; then
    echo "⚠️  Container $VMID already exists!"
    echo ""
    read -p "Delete and recreate? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        echo "Stopping and removing container $VMID..."
        ssh root@10.0.10.3 "pct stop $VMID 2>/dev/null || true"
        sleep 2
        ssh root@10.0.10.3 "pct destroy $VMID"
        echo "✅ Container removed"
    else
        echo "❌ Aborted"
        exit 1
    fi
fi

echo "Creating LXC container..."
ssh root@10.0.10.3 << EOF
pct create $VMID $TEMPLATE \
  --hostname $HOSTNAME \
  --cores $CORES \
  --memory $MEMORY \
  --swap $SWAP \
  --net0 name=eth0,bridge=vmbr0,ip=$IP/24,gw=$GATEWAY \
  --storage local-lvm \
  --rootfs 8 \
  --unprivileged 1 \
  --features nesting=1

echo "✅ Container created"
echo ""

echo "Starting container..."
pct start $VMID
sleep 5

echo "✅ Container started"
echo ""

echo "Installing Docker..."
pct exec $VMID -- bash -c '
apt update
apt install -y docker.io curl
systemctl enable docker
systemctl start docker
'

echo "✅ Docker installed"
echo ""

echo "Deploying Uptime Kuma..."
pct exec $VMID -- bash -c "
docker run -d \
  --name uptime-kuma \
  --restart=always \
  -p 3001:3001 \
  -v uptime-kuma:/app/data \
  louislam/uptime-kuma:2

echo 'Waiting for Uptime Kuma to start...'
sleep 10

# Verify container is running
if docker ps | grep -q uptime-kuma; then
    echo '✅ Uptime Kuma container running'
else
    echo '❌ Uptime Kuma failed to start'
    docker logs uptime-kuma
    exit 1
fi
"

echo ""
echo "Testing Uptime Kuma endpoint..."
sleep 5
EOF

# Test from host
if curl -s -o /dev/null -w "%{http_code}" http://$IP:3001 | grep -q 200; then
    echo "✅ Uptime Kuma is responding!"
else
    echo "⚠️  Uptime Kuma not responding yet (may need more time to start)"
fi

echo ""
echo "=== Deployment Complete! ==="
echo ""
echo "🎉 Uptime Kuma deployed successfully!"
echo ""
echo "Access Uptime Kuma:"
echo "  URL: http://$IP:3001"
echo "  Initial setup required on first visit"
echo ""
echo "Next steps:"
echo "1. Go to http://$IP:3001"
echo "2. Create admin account"
echo "3. Add monitors for your services"
echo "4. Create status page"
echo ""
echo "Container management:"
echo "  Start:   ssh root@10.0.10.3 'pct start $VMID'"
echo "  Stop:    ssh root@10.0.10.3 'pct stop $VMID'"
echo "  Shell:   ssh root@10.0.10.3 'pct enter $VMID'"
echo "  Logs:    ssh root@10.0.10.3 'pct exec $VMID -- docker logs uptime-kuma -f'"
echo ""
