name: Staging deploy

on:
  push:
    branches: ["develop"]

jobs:
  docker-build-and-push-client:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build Client Docker image
        run: |
          docker build \
            -t ghcr.io/bluewave-labs/checkmate:frontend-staging \
            -f ./docker/staging/client.Dockerfile \
            --label org.opencontainers.image.source=https://github.com/bluewave-labs/checkmate \
            .

      - name: Push Client Docker image
        run: docker push ghcr.io/bluewave-labs/checkmate:frontend-staging

  docker-build-and-push-server:
    needs: docker-build-and-push-client
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Log in to GitHub Container Registry
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build Server Docker image
        run: |
          docker build \
            -t ghcr.io/bluewave-labs/checkmate:backend-staging \
            -f ./docker/staging/server.Dockerfile \
            --label org.opencontainers.image.source=https://github.com/bluewave-labs/checkmate \
            .

      - name: Push Server Docker image
        run: docker push ghcr.io/bluewave-labs/checkmate:backend-staging

      - name: Build Mongo Docker image
        run: |
          docker build \
            -t ghcr.io/bluewave-labs/checkmate:mongo-staging \
            -f ./docker/staging/mongoDB.Dockerfile \
            --label org.opencontainers.image.source=https://github.com/bluewave-labs/checkmate \
            .

      - name: Push MongoDB Docker image
        run: docker push ghcr.io/bluewave-labs/checkmate:mongo-staging

      - name: Build Redis Docker image
        run: |
          docker build \
            -t ghcr.io/bluewave-labs/checkmate:redis-staging \
            -f ./docker/staging/redis.Dockerfile \
            --label org.opencontainers.image.source=https://github.com/bluewave-labs/checkmate \
            .

      - name: Push Redis Docker image
        run: docker push ghcr.io/bluewave-labs/checkmate:redis-staging

  deploy-to-staging:
    needs: docker-build-and-push-server
    runs-on: ubuntu-latest
    steps:
      - name: SSH into server and restart container using Docker Compose
        uses: appleboy/ssh-action@v1.2.2
        with:
          host: ${{ secrets.STAGING_SERVER_HOST }}
          username: ${{ secrets.STAGING_SERVER_USER }}
          key: ${{ secrets.STAGING_SERVER_SSH_KEY }}
          script: |
            cd checkmate
            git pull
            cd docker/staging
            docker compose down
            docker compose pull
            docker compose up -d
