name: Deploy Brew Application

on:
  push:
    branches:
      - master 

jobs:
  deploy:
    runs-on: production
    
    steps:
      - name: Checkout Code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Build Frontend
        env:
          CI: "true"
        run: |
          npm install
          rm -rf dist/
          npm run build

      - name: Deploy Frontend Files
        run: |
          rm -rf /var/www/brew/dist/
          cp -r dist/ /var/www/brew/

      - name: Restart Backend with PM2
        run: |
          cd server
          npm install
          pm2 restart brew-backend || pm2 start index.js --name "brew-backend"