name: Deploy Impler to Production

on:
  push:
    branches:
      - main

jobs:
  build_api:
    permissions:
      contents: read
      packages: write 
      deployments: write
    uses: ./.github/workflows/build-api.yml
    secrets: inherit

  build_embed:
    permissions:
      contents: read
      packages: write
      deployments: write
    uses: ./.github/workflows/build-embed.yml
    secrets: inherit

  build_queuemanager:
    permissions:
      contents: read
      packages: write
      deployments: write
    uses: ./.github/workflows/build-queuemanager.yml
    secrets: inherit

  build_web:
    permissions:
      contents: read
      packages: write
      deployments: write
    uses: ./.github/workflows/build-web.yml
    secrets: inherit

  build_widget:
    permissions:
      contents: read
      packages: write
      deployments: write
    uses: ./.github/workflows/build-widget.yml
    secrets: inherit

  tag_images:
    needs:
    - build_api
    - build_embed
    - build_queuemanager
    - build_web
    - build_widget
    if: |
      always() && 
      (needs.build_api.result == 'success' && needs.build_embed.result == 'success' && needs.build_queuemanager.result == 'success' && needs.build_web.result == 'success' && needs.build_widget.result == 'success')
    permissions:
      contents: read
      packages: write
      deployments: write
    uses: ./.github/workflows/tag-images.yml
    secrets: inherit

  publish_npm:
    needs:
    - tag_images
    if: |
      always() && 
      (needs.tag_images.result == 'success')
    permissions:
      contents: read
      packages: write
      deployments: write
    uses: ./.github/workflows/publish-prod-packages.yml
    secrets: inherit

  # deploy_with_ssh:
  #   runs-on: ubuntu-latest
  #   needs:
  #     - publish_npm
  #   if: |
  #     always() && 
  #     (needs.publish_npm.result == 'success')
  #   steps:
  #     - name: Set up SSH and Run Docker Compose Up
  #       uses: webfactory/ssh-agent@v0.5.3
  #       with:
  #         ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY_QA }}
  #     - name: Run Docker Compose Up
  #       run: |
  #         ssh -o StrictHostKeyChecking=no ${{ secrets.SSH_USERNAME_QA }}@${{ secrets.SSH_HOST_QA }} \
  #           "cd ${{ secrets.SSH_PATH_QA }} && sudo docker compose up --pull always -d && sudo docker image prune -a -f"
