name: Reusable check images tags and push
on:
  workflow_call:
    inputs:
      commit_tag:
        required: true
        type: string
      last_commit_tag:
        required: true
        type: string
      develop_tag:
        required: true
        type: string
      coordinator_changed:
        required: true
        type: string
      postman_changed:
        required: true
        type: string
      prover_changed:
        required: true
        type: string
      transaction_exclusion_api_changed:
        required: true
        type: string
    outputs:
      image_tagged_coordinator:
        value: ${{ jobs.image_tag_push.outputs.image_tagged_coordinator }}
      image_tagged_prover:
        value: ${{ jobs.image_tag_push.outputs.image_tagged_prover }}
      image_tagged_postman:
        value: ${{ jobs.image_tag_push.outputs.image_tagged_postman }}
      image_tagged_transaction_exclusion_api:
        value: ${{ jobs.image_tag_push.outputs.image_tagged_transaction_exclusion_api }}
    secrets:
      DOCKERHUB_USERNAME:
        required: false
      DOCKERHUB_TOKEN:
        required: false

concurrency:
  group: check-images-tags-and-push-${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  check_image_tags_exist:
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-small
    name: Check image tags exist
    outputs:
      last_commit_tag_exists_coordinator: ${{ steps.check_image_tags_exist_coordinator.outputs.last_commit_tag_exists }}
      last_commit_tag_exists_postman: ${{ steps.check_image_tags_exist_postman.outputs.last_commit_tag_exists }}
      last_commit_tag_exists_prover: ${{ steps.check_image_tags_exist_prover.outputs.last_commit_tag_exists }}
      last_commit_tag_exists_transaction_exclusion_api: ${{ steps.check_image_tags_exist_transaction_exclusion_api.outputs.last_commit_tag_exists }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Check image tags exist for coordinator
        uses: ./.github/actions/check-image-tags-exist
        if: ${{ inputs.coordinator_changed == 'false' }}
        id: check_image_tags_exist_coordinator
        with:
          last_commit_tag: ${{ inputs.last_commit_tag }}
          image_name: consensys/linea-coordinator

      - name: Check image tags exist for postman
        uses: ./.github/actions/check-image-tags-exist
        if: ${{ inputs.postman_changed == 'false' }}
        id: check_image_tags_exist_postman
        with:
          last_commit_tag: ${{ inputs.last_commit_tag }}
          image_name: consensys/linea-postman

      - name: Check image tags exist for prover
        uses: ./.github/actions/check-image-tags-exist
        if: ${{ inputs.prover_changed == 'false' }}
        id: check_image_tags_exist_prover
        with:
          last_commit_tag: ${{ inputs.last_commit_tag }}
          image_name: consensys/linea-prover

      - name: Check image tags exist for transaction-exclusion-api
        uses: ./.github/actions/check-image-tags-exist
        if: ${{ inputs.transaction_exclusion_api_changed == 'false' }}
        id: check_image_tags_exist_transaction_exclusion_api
        with:
          last_commit_tag: ${{ inputs.last_commit_tag }}
          image_name: consensys/linea-transaction-exclusion-api

  image_tag_push:
    runs-on: gha-runner-scale-set-ubuntu-22.04-amd64-small
    if: ${{ github.ref == 'refs/heads/main' }}
    name: Tag and push images
    needs: [ check_image_tags_exist ]
    outputs:
      image_tagged_coordinator: ${{ steps.image_tag_push_coordinator.outputs.image_tagged }}
      image_tagged_prover: ${{ steps.image_tag_push_prover.outputs.image_tagged }}
      image_tagged_postman: ${{ steps.image_tag_push_postman.outputs.image_tagged }}
      image_tagged_transaction_exclusion_api: ${{ steps.image_tag_push_transaction_exclusion_api.outputs.image_tagged }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Tag and push coordinator image
        id: image_tag_push_coordinator
        uses: ./.github/actions/image-tag-and-push
        if: ${{ inputs.coordinator_changed == 'false' }}
        with:
          commit_tag: ${{ inputs.commit_tag }}
          last_commit_tag: ${{ inputs.last_commit_tag }}
          develop_tag: ${{ inputs.develop_tag }}
          image_name: consensys/linea-coordinator
          last_commit_tag_exists: ${{ needs.check_image_tags_exist.outputs.last_commit_tag_exists_coordinator }}
          docker_username: ${{ secrets.DOCKERHUB_USERNAME }}
          docker_password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Tag and push postman image
        id: image_tag_push_postman
        uses: ./.github/actions/image-tag-and-push
        if: ${{ inputs.postman_changed == 'false' }}
        with:
          commit_tag: ${{ inputs.commit_tag }}
          last_commit_tag: ${{ inputs.last_commit_tag }}
          develop_tag: ${{ inputs.develop_tag }}
          image_name: consensys/linea-postman
          last_commit_tag_exists: ${{ needs.check_image_tags_exist.outputs.last_commit_tag_exists_postman }}
          docker_username: ${{ secrets.DOCKERHUB_USERNAME }}
          docker_password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Tag and push prover image
        id: image_tag_push_prover
        uses: ./.github/actions/image-tag-and-push
        if: ${{ inputs.prover_changed == 'false' }}
        with:
          commit_tag: ${{ inputs.commit_tag }}
          last_commit_tag: ${{ inputs.last_commit_tag }}
          develop_tag: ${{ inputs.develop_tag }}
          image_name: consensys/linea-prover
          last_commit_tag_exists: ${{ needs.check_image_tags_exist.outputs.last_commit_tag_exists_prover }}
          docker_username: ${{ secrets.DOCKERHUB_USERNAME }}
          docker_password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Tag and push transaction exclusion api image
        id: image_tag_push_transaction_exclusion_api
        uses: ./.github/actions/image-tag-and-push
        if: ${{ inputs.transaction_exclusion_api_changed == 'false' }}
        with:
          commit_tag: ${{ inputs.commit_tag }}
          last_commit_tag: ${{ inputs.last_commit_tag }}
          develop_tag: ${{ inputs.develop_tag }}
          image_name: consensys/linea-transaction-exclusion-api
          last_commit_tag_exists: ${{ needs.check_image_tags_exist.outputs.last_commit_tag_exists_transaction_exclusion_api }}
          docker_username: ${{ secrets.DOCKERHUB_USERNAME }}
          docker_password: ${{ secrets.DOCKERHUB_TOKEN }}
