name: Test and deploy
on:
  push:
    branches: [dev]
  pull_request:
    branches: [dev]
jobs:
  test-backend:
    name: Test Backend
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18.x]
        mongodb-version: ['4.4']
    steps:
      - name: Git checkout
        uses: actions/checkout@v2

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: Start MongoDB
        uses: supercharge/mongodb-github-action@1.6.0
        with:
          mongodb-version: ${{ matrix.mongodb-version }}

      - name: Install Backend Packages
        run: npm ci -f
        working-directory: ./backend

      - name: Test Backend
        run: npm test
        working-directory: ./backend
        env:
          MONGO_URI: mongodb://localhost/accord-test
          NODE_ENV: dev
          PORT: 3001
          WEBSITE_URL: http://localhost:4200

  test-frontend:
    name: Test Frontend
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18.x]
        mongodb-version: ['4.4']
    steps:
      - name: Git checkout
        uses: actions/checkout@v2

      - name: Install Backend Packages
        run: npm ci -f
        working-directory: ./backend

      - name: Start Backend for Frontend e2e
        run: npm start &
        working-directory: ./backend
        env:
          MONGO_URI: mongodb://localhost/accord
          NODE_ENV: dev
          PORT: 3000
          WEBSITE_URL: http://localhost:4200

      - name: Install Frontend Packages
        run: npm ci
        working-directory: ./frontend

      # FIXME:
      # - name: Test Frontend (Unit)
      #   run: npm start && sleep 20 && npm run test:unit
      #   working-directory: ./frontend

      - name: Test Frontend (e2e)
        uses: cypress-io/github-action@v2
        with:
          browser: chrome
          headless: true
          start: npm start
          wait-on: http://localhost:3000, http://localhost:4200
          working-directory: ./frontend

  deploy-frontend:
    name: Deploy Frontend
    runs-on: ubuntu-latest
    needs: [test-backend]
    steps:
      - name: Git checkout
        uses: actions/checkout@v2

      - name: Install Frontend Packages
        run: npm ci -f && npm cache clean -f
        working-directory: ./frontend

      - name: Build Frontend
        run: npm run build:prod
        working-directory: ./frontend
        env:
          REACT_APP_VERSION: ${{ github.sha }}

      - name: Check index.html
        id: check-files
        uses: andstor/file-existence-action@v1
        with:
          files: index.html

      - name: Deploy Frontend
        if: steps.check-files.outputs.files-exists == 'true'
        uses: JamesIves/github-pages-deploy-action@4.1.5
        with:
          branch: gh-pages
          folder: frontend/build
          clean: true
          clean-exclude: |
            CNAME
            404.html
            redirect-script.html

  push-to-stable:
    name: Push to Stable
    runs-on: ubuntu-latest
    needs: [test-backend]
    steps:
      - name: Git checkout
        uses: actions/checkout@v2

      - name: Push to stable
        run: |
          git config --global user.name 'Your Name' 
          git config --global user.email 'Your Email for GitHub'
          git push origin HEAD:stable
