---
name: Build and Release
on:
  workflow_call:
    inputs:
      version:
        required: true
        type: string
    secrets:
      HOMEBREW_PAT:
        required: true
      AUR_SECRET_KEY:
        required: true
      NUR_PAT:
        required: true
permissions:
  contents: write
jobs:
  build-macos:
    name: Build macOS Binaries
    runs-on: macos-latest
    strategy:
      matrix:
        target: [aarch64-apple-darwin, x86_64-apple-darwin]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.version }}
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Add target
        run: rustup target add ${{ matrix.target }}
      - name: Build binary
        run: cargo build --release --target ${{ matrix.target }}
      - name: Rename and compress binary
        run: |
          mkdir -p bin
          cp target/${{ matrix.target }}/release/tempesta bin/tempesta
          tar -czvf bin/tempesta-${{ matrix.target }}.tar.gz -C bin tempesta
      - name: Upload binary as artifact
        uses: actions/upload-artifact@v4
        with:
          name: tempesta-${{ matrix.target }}
          path: bin/tempesta-${{ matrix.target }}.tar.gz

  build-linux:
    name: Build Linux Binaries
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: [x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.version }}
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Install cross
        run: cargo install cross
      - name: Add target
        run: rustup target add ${{ matrix.target }}
      - name: Build binary with cross
        run: cross build --release --target ${{ matrix.target }}
      - name: Rename and compress binary
        run: |
          mkdir -p bin
          cp target/${{ matrix.target }}/release/tempesta bin/tempesta
          tar -czvf bin/tempesta-${{ matrix.target }}.tar.gz -C bin tempesta
      - name: Upload binary as artifact
        uses: actions/upload-artifact@v4
        with:
          name: tempesta-${{ matrix.target }}
          path: bin/tempesta-${{ matrix.target }}.tar.gz

  github-release:
    name: Create GitHub Release
    needs: [build-macos, build-linux]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.version }}
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: bin
          merge-multiple: true
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ inputs.version }}
          files: bin/**
          token: ${{ github.token }}
          draft: false
          prerelease: false
      - name: Trigger Homebrew Tap Update
        run: |
          curl -X POST -H "Accept: application/vnd.github.v3+json" \
               -H "Authorization: token ${{ secrets.HOMEBREW_PAT }}" \
               https://api.github.com/repos/x71c9/homebrew-x71c9/dispatches \
               -d '{"event_type": "update-tempesta", "client_payload": {"tag": "${{ inputs.version }}"}}'

  update-package-repos:
    name: Update Package Repositories
    needs: github-release
    uses: ./.github/workflows/update-packages.yml
    with:
      version: ${{ inputs.version }}
    secrets: inherit