#!/bin/bash

# Script para crear la VM en Google Cloud
# Ejecutar este script desde tu máquina local con gcloud CLI instalado

PROJECT_ID="tu-proyecto-gcloud"  # Cambiar por tu project ID
ZONE="us-central1-a"
VM_NAME="snatchgame-vm"
MACHINE_TYPE="e2-micro"
IMAGE_FAMILY="ubuntu-2004-lts"
IMAGE_PROJECT="ubuntu-os-cloud"

echo "Creando VM para SnatchGame en Google Cloud..."

# Crear la VM
gcloud compute instances create $VM_NAME \
    --project=$PROJECT_ID \
    --zone=$ZONE \
    --machine-type=$MACHINE_TYPE \
    --network-interface=network-tier=PREMIUM,subnet=default \
    --maintenance-policy=MIGRATE \
    --provisioning-model=STANDARD \
    --service-account=default \
    --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append \
    --tags=http-server,https-server \
    --create-disk=auto-delete=yes,boot=yes,device-name=$VM_NAME,image=projects/$IMAGE_PROJECT/global/images/family/$IMAGE_FAMILY,mode=rw,size=20,type=projects/$PROJECT_ID/zones/$ZONE/diskTypes/pd-standard \
    --no-shielded-secure-boot \
    --shielded-vtpm \
    --shielded-integrity-monitoring \
    --labels=environment=production,app=snatchgame \
    --reservation-affinity=any \
    --metadata-from-file startup-script=startup-script.sh

echo "VM creada exitosamente!"

# Obtener IP externa
EXTERNAL_IP=$(gcloud compute instances describe $VM_NAME --zone=$ZONE --format='get(networkInterfaces[0].accessConfigs[0].natIP)')

echo ""
echo "=================================================="
echo "VM SnatchGame creada exitosamente!"
echo "=================================================="
echo "Nombre: $VM_NAME"
echo "Zona: $ZONE"
echo "IP Externa: $EXTERNAL_IP"
echo "=================================================="
echo ""
echo "Próximos pasos:"
echo "1. Configurar DNS en Cloudflare:"
echo "   A record: snatchgame.nucleoriofrio.com → $EXTERNAL_IP"
echo ""
echo "2. Esperar ~5 minutos para que el startup script termine"
echo ""
echo "3. Verificar que funciona:"
echo "   http://$EXTERNAL_IP"
echo ""
echo "4. Una vez que DNS se propague:"
echo "   http://snatchgame.nucleoriofrio.com"
echo ""
echo "Para monitorear el progreso del startup script:"
echo "gcloud compute ssh $VM_NAME --zone=$ZONE --command='sudo tail -f /var/log/syslog'"