name: Deploy to Production

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Deploy to nexus-vector
        uses: appleboy/ssh-action@master
        with:
          host: nexus-vector
          username: admin
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd /srv/containers/ca-grow-ops-manager
            
            echo "📥 Pulling latest code..."
            git pull origin main
            
            echo "🔨 Rebuilding containers..."
            docker compose build --no-cache
            
            echo "🗄️ Running database migrations..."
            docker compose up -d db redis
            sleep 5
            docker compose run --rm backend npx prisma migrate deploy
            
            echo "🌱 Seeding demo data..."
            docker compose run --rm backend node prisma/seed.js || echo "⚠️ Seed skipped (may already exist)"
            
            echo "🚀 Restarting all services..."
            docker compose up -d
            
            echo "✅ Deployment complete!"
            docker compose ps
            
            echo "🏥 Health check..."
            sleep 10
            curl -f http://localhost:8010/api/healthz || echo "⚠️ Health check failed"
