---
name: 'build darwin python backend container images (reusable)'

on:
  workflow_call:
    inputs:
      backend:
        description: 'Backend to build'
        required: true
        type: string
      build-type:
        description: 'Build type (e.g., mps)'
        default: ''
        type: string
      use-pip:
        description: 'Use pip to install dependencies'
        default: false
        type: boolean
      lang:
        description: 'Programming language (e.g. go)'
        default: 'python'
        type: string
      go-version:
        description: 'Go version to use'
        default: '1.24.x'
        type: string
      tag-suffix:
        description: 'Tag suffix for the built image'
        required: true
        type: string
      runs-on:
        description: 'Runner to use'
        default: 'macOS-14'
        type: string
    secrets:
      dockerUsername:
        required: false
      dockerPassword:
        required: false
      quayUsername:
        required: true
      quayPassword:
        required: true

jobs:
  darwin-backend-build:
    runs-on: ${{ inputs.runs-on }}
    strategy:
      matrix:
        go-version: ['${{ inputs.go-version }}']
    steps:
      - name: Clone
        uses: actions/checkout@v5
        with:
          submodules: true

      - name: Setup Go ${{ matrix.go-version }}
        uses: actions/setup-go@v5
        with:
          go-version: ${{ matrix.go-version }}
          cache: false

      # You can test your matrix by printing the current Go version
      - name: Display Go version
        run: go version

      - name: Dependencies
        run: |
          brew install protobuf grpc make protoc-gen-go protoc-gen-go-grpc libomp llvm

      - name: Build ${{ inputs.backend }}-darwin
        run: |
          make protogen-go
          BACKEND=${{ inputs.backend }} BUILD_TYPE=${{ inputs.build-type }} USE_PIP=${{ inputs.use-pip }} make build-darwin-${{ inputs.lang }}-backend

      - name: Upload ${{ inputs.backend }}.tar
        uses: actions/upload-artifact@v4
        with:
          name: ${{ inputs.backend }}-tar
          path: backend-images/${{ inputs.backend }}.tar

  darwin-backend-publish:
    needs: darwin-backend-build
    if: github.event_name != 'pull_request'
    runs-on: ubuntu-latest
    steps:
      - name: Download ${{ inputs.backend }}.tar
        uses: actions/download-artifact@v5
        with:
          name: ${{ inputs.backend }}-tar
          path: .

      - name: Install crane
        run: |
          curl -L https://github.com/google/go-containerregistry/releases/latest/download/go-containerregistry_Linux_x86_64.tar.gz | tar -xz
          sudo mv crane /usr/local/bin/

      - name: Log in to DockerHub
        run: |
          echo "${{ secrets.dockerPassword }}" | crane auth login docker.io -u "${{ secrets.dockerUsername }}" --password-stdin

      - name: Log in to quay.io
        run: |
          echo "${{ secrets.quayPassword }}" | crane auth login quay.io -u "${{ secrets.quayUsername }}" --password-stdin

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: |
            localai/localai-backends
          tags: |
            type=ref,event=branch
            type=semver,pattern={{raw}}
            type=sha
          flavor: |
            latest=auto
            suffix=${{ inputs.tag-suffix }},onlatest=true

      - name: Docker meta
        id: quaymeta
        uses: docker/metadata-action@v5
        with:
          images: |
            quay.io/go-skynet/local-ai-backends
          tags: |
            type=ref,event=branch
            type=semver,pattern={{raw}}
            type=sha
          flavor: |
            latest=auto
            suffix=${{ inputs.tag-suffix }},onlatest=true

      - name: Push Docker image (DockerHub)
        run: |
          for tag in $(echo "${{ steps.meta.outputs.tags }}" | tr ',' '\n'); do
            crane push ${{ inputs.backend }}.tar $tag
          done

      - name: Push Docker image (Quay)
        run: |
          for tag in $(echo "${{ steps.quaymeta.outputs.tags }}" | tr ',' '\n'); do
            crane push ${{ inputs.backend }}.tar $tag
          done
