#!/bin/bash

# Startup script para VM de Google Cloud
# Este script se ejecuta cuando la VM inicia

# Update system
apt-get update
apt-get install -y apt-transport-https ca-certificates curl software-properties-common

# Install Docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io

# Install Docker Compose
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

# Install Nginx
apt-get install -y nginx

# Create nginx config for SnatchGame
cat > /etc/nginx/sites-available/snatchgame << 'EOF'
server {
    listen 80;
    server_name snatchgame.nucleoriofrio.com;
    client_max_body_size 100M;

    # Frontend (puerto 3000 del contenedor)
    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
    }

    # WebSocket de Colyseus
    location /ws {
        proxy_pass http://localhost:2567;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # Headers específicos para WebSocket
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_cache_bypass $http_upgrade;
        
        # Timeouts para WebSocket
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    # API REST general
    location /api/ {
        proxy_pass http://localhost:2567/api/;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
    }

    # SSE para dashboard
    location /api/dashboard-stream {
        proxy_pass http://localhost:2567/api/dashboard-stream;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # Headers específicos para SSE
        proxy_buffering off;
        proxy_cache off;
        proxy_read_timeout 24h;
        proxy_http_version 1.1;
    }

    # Monitor de Colyseus (opcional)
    location /colyseus {
        proxy_pass http://localhost:2567/colyseus;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
    }
}
EOF

# Enable site
ln -sf /etc/nginx/sites-available/snatchgame /etc/nginx/sites-enabled/
rm -f /etc/nginx/sites-enabled/default

# Start services
systemctl enable nginx
systemctl start nginx
systemctl enable docker
systemctl start docker

# Create deployment script
cat > /opt/deploy-snatchgame.sh << 'EOF'
#!/bin/bash

# Script para desplegar nueva versión del juego
echo "Deploying SnatchGame..."

# Parar contenedor anterior
docker stop snatchgame 2>/dev/null || true
docker rm snatchgame 2>/dev/null || true

# Obtener última imagen
docker pull gitea.nucleoriofrio.com/nucleo000/snatchgame:latest

# Ejecutar nuevo contenedor
docker run -d \
  --name snatchgame \
  --restart unless-stopped \
  -p 3000:3000 \
  -p 2567:2567 \
  gitea.nucleoriofrio.com/nucleo000/snatchgame:latest

echo "SnatchGame deployed successfully!"
EOF

chmod +x /opt/deploy-snatchgame.sh

# Initial deployment
/opt/deploy-snatchgame.sh

echo "VM setup completed!"