#!/bin/bash
# deploy.sh — First-time setup for CentOS 7 VPS
# Usage: bash deploy.sh

set -e

APP_DIR="/var/www/saao-machinery-catalogue"
DOMAIN="saao-machinery.com"
NODE_VERSION="20"

echo "=== SAAO Machinery Catalogue — VPS Setup ==="

# 1. Install nvm + Node.js 20 (if not present)
if ! command -v node &>/dev/null || [[ "$(node -v)" < "v18" ]]; then
  echo "Installing Node.js $NODE_VERSION via nvm..."
  curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
  export NVM_DIR="$HOME/.nvm"
  source "$NVM_DIR/nvm.sh"
  nvm install $NODE_VERSION
  nvm use $NODE_VERSION
  nvm alias default $NODE_VERSION
else
  echo "Node.js $(node -v) already installed."
fi

# Ensure nvm is loaded in this shell
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh"

# 2. Install PM2 globally
if ! command -v pm2 &>/dev/null; then
  echo "Installing PM2..."
  npm install -g pm2
fi

# 3. Install nginx via EPEL (CentOS 7)
if ! command -v nginx &>/dev/null; then
  echo "Installing nginx..."
  yum install -y epel-release
  yum install -y nginx
  systemctl enable nginx
fi

# 4. App dependencies + build
echo "Building app in $APP_DIR..."
cd "$APP_DIR"

# Create .env.local if it doesn't exist
if [ ! -f .env.local ]; then
  cp .env.example .env.local
  echo ""
  echo "⚠  .env.local created from template."
  echo "   Edit it and set NEXT_PUBLIC_BASE_URL to your domain, then re-run: pm2 restart saao-machinery"
  echo ""
fi

npm ci --production=false
npm run build

# 5. Start with PM2
pm2 delete saao-machinery 2>/dev/null || true
pm2 start ecosystem.config.js
pm2 save
pm2 startup systemd -u root --hp /root | tail -1 | bash 2>/dev/null || true

# 6. Write nginx config
NGINX_CONF="/etc/nginx/conf.d/saao-machinery.conf"
cat > "$NGINX_CONF" <<NGINX
server {
    listen 80;
    server_name $DOMAIN www.$DOMAIN;

    # Redirect www → non-www
    if (\$host = www.$DOMAIN) {
        return 301 https://$DOMAIN\$request_uri;
    }

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host \$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_cache_bypass \$http_upgrade;
    }

    # Static assets — long cache
    location /_next/static/ {
        proxy_pass http://127.0.0.1:3000;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }

    location /images/ {
        proxy_pass http://127.0.0.1:3000;
        add_header Cache-Control "public, max-age=86400";
    }
}
NGINX

nginx -t && systemctl restart nginx

echo ""
echo "=== Done! ==="
echo "App running at http://$DOMAIN"
echo ""
echo "Next steps:"
echo "  1. Edit .env.local with your domain and GA4 ID"
echo "  2. Point your domain DNS A record to this server's IP"
echo "  3. Install SSL: yum install -y certbot python2-certbot-nginx && certbot --nginx -d $DOMAIN -d www.$DOMAIN"
echo ""
echo "Useful commands:"
echo "  pm2 status              — check app status"
echo "  pm2 logs saao-machinery — view logs"
echo "  pm2 restart saao-machinery — restart after .env changes"
