name: Docker Build and Push
on:
  # Trigger after RELEASE workflow completes
  workflow_run:
    workflows: ["RELEASE"]
    types:
      - completed
    branches:
      - main
  # Keep existing triggers for flexibility
  # push:
  #   branches:
  #     - main
  # workflow_dispatch:

jobs:
  # Detect which modules have changed
  changes:
    runs-on: ubuntu-latest
    outputs:
      talk2biomodels: ${{ steps.filter.outputs.talk2biomodels }}
      talk2scholars: ${{ steps.filter.outputs.talk2scholars }}
      talk2aiagents4pharma: ${{ steps.filter.outputs.talk2aiagents4pharma }}
      talk2knowledgegraphs: ${{ steps.filter.outputs.talk2knowledgegraphs }}
      app: ${{ steps.filter.outputs.app }}

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Check for changes
        uses: dorny/paths-filter@v2
        id: filter
        with:
          filters: |
            talk2biomodels:
              - 'aiagents4pharma/talk2biomodels/**'
            talk2scholars:
              - 'aiagents4pharma/talk2scholars/**'
            talk2aiagents4pharma:
              - 'aiagents4pharma/talk2aiagents4pharma/**'
            talk2knowledgegraphs:
              - 'aiagents4pharma/talk2knowledgegraphs/**'
            app:
              - 'app/**'

  # Generate version for the build
  version:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get_version.outputs.version }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      # Get the latest version from git tags

      - name: Get latest version tag
        id: get_version
        run: |
          # Get the latest tag from git
          LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.1.0")

          # Always use the latest tag directly (no dev builds)
          VERSION=$LATEST_TAG

          echo "Using version: $VERSION"
          echo "version=$VERSION" >> $GITHUB_OUTPUT

  # Separate job for talk2aiagents4pharma
  build-talk2aiagents4pharma:
    needs: [changes, version]
    if: ${{ needs.changes.outputs.talk2aiagents4pharma == 'true' || needs.changes.outputs.talk2biomodels == 'true' || needs.changes.outputs.talk2knowledgegraphs == 'true' || needs.changes.outputs.app == 'true' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ vars.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v6
        with:
          file: aiagents4pharma/talk2aiagents4pharma/Dockerfile
          push: true
          tags: |
            virtualpatientengine/talk2aiagents4pharma:${{ needs.version.outputs.version }}
            virtualpatientengine/talk2aiagents4pharma:latest

  # Separate job for talk2biomodels
  build-talk2biomodels:
    needs: [changes, version]
    if: ${{ needs.changes.outputs.talk2biomodels == 'true' || needs.changes.outputs.app == 'true' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ vars.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v6
        with:
          file: aiagents4pharma/talk2biomodels/Dockerfile
          push: true
          tags: |
            virtualpatientengine/talk2biomodels:${{ needs.version.outputs.version }}
            virtualpatientengine/talk2biomodels:latest

  # Separate job for talk2scholars
  build-talk2scholars:
    needs: [changes, version]
    if: ${{ needs.changes.outputs.talk2scholars == 'true' || needs.changes.outputs.app == 'true' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ vars.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v6
        with:
          file: aiagents4pharma/talk2scholars/Dockerfile
          push: true
          tags: |
            virtualpatientengine/talk2scholars:${{ needs.version.outputs.version }}
            virtualpatientengine/talk2scholars:latest

  # Separate job for talk2knowledgegraphs
  build-talk2knowledgegraphs:
    needs: [changes, version]
    if: ${{ needs.changes.outputs.talk2knowledgegraphs == 'true' || needs.changes.outputs.app == 'true' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ vars.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and Push Docker Image
        uses: docker/build-push-action@v6
        with:
          file: aiagents4pharma/talk2knowledgegraphs/Dockerfile
          push: true
          tags: |
            virtualpatientengine/talk2knowledgegraphs:${{ needs.version.outputs.version }}
            virtualpatientengine/talk2knowledgegraphs:latest
