name: Docker Image CI

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]
  workflow_dispatch:

env:
  REGISTRY: ghcr.io
  IMAGE_NAME: miuda-ai/active-call

jobs:
  build-amd64:
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install Cross
        uses: taiki-e/install-action@v2
        with:
          tool: cross

      - name: Build AMD64 binary
        run: |
          cross build --release --target x86_64-unknown-linux-gnu --no-default-features --features cross --bin active-call

          mkdir -p bin/amd64
          install -m 0755 target/x86_64-unknown-linux-gnu/release/active-call bin/amd64/active-call

      - name: Upload AMD64 binary
        uses: actions/upload-artifact@v4
        with:
          name: bin-amd64
          path: bin/amd64
          if-no-files-found: error

  build-arm64:
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - uses: actions/checkout@v6

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install Cross
        uses: taiki-e/install-action@v2
        with:
          tool: cross

      - name: Build ARM64 binary
        run: |
          cross build --release --target aarch64-unknown-linux-gnu --no-default-features --features cross --bin active-call

          mkdir -p bin/arm64
          install -m 0755 target/aarch64-unknown-linux-gnu/release/active-call bin/arm64/active-call

      - name: Upload ARM64 binary
        uses: actions/upload-artifact@v4
        with:
          name: bin-arm64
          path: bin/arm64
          if-no-files-found: error

  docker-image:
    runs-on: ubuntu-latest
    needs:
      - build-amd64
      - build-arm64
    permissions:
      contents: read
      packages: write

    steps:
      - uses: actions/checkout@v6

      - name: Download AMD64 binary
        uses: actions/download-artifact@v4
        with:
          name: bin-amd64
          path: bin/amd64

      - name: Download ARM64 binary
        uses: actions/download-artifact@v4
        with:
          name: bin-arm64
          path: bin/arm64

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to Container registry
        if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
        uses: docker/login-action@v3
        with:
          registry: ${{ env.REGISTRY }}
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          platforms: linux/amd64,linux/arm64
          push: ${{ github.event_name == 'workflow_dispatch' || (github.event_name == 'push' && contains(github.event.head_commit.message, '[latest]')) }}
          tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
          cache-from: type=gha
          cache-to: type=gha,mode=max
          provenance: false
          sbom: false
