name: Release

on:
  push:
    tags:
      - 'v*'

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          # Linux x86_64
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            artifact_name: octoroute
            asset_name: octoroute-linux-x86_64

          # Linux ARM64 (using cross)
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            artifact_name: octoroute
            asset_name: octoroute-linux-aarch64
            use_cross: true

          # macOS x86_64 (Intel) - use macos-13 which is still Intel
          - target: x86_64-apple-darwin
            os: macos-13
            artifact_name: octoroute
            asset_name: octoroute-macos-x86_64

          # macOS ARM64 (Apple Silicon) - macos-latest is now ARM64
          - target: aarch64-apple-darwin
            os: macos-latest
            artifact_name: octoroute
            asset_name: octoroute-macos-aarch64

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cross
        if: matrix.use_cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build release binary
        if: ${{ !matrix.use_cross }}
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build release binary (cross)
        if: matrix.use_cross
        run: cross build --release --target ${{ matrix.target }}

      - name: Create tarball (Unix)
        run: |
          cd target/${{ matrix.target }}/release
          tar -czvf ../../../${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
          cd ../../..

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: ${{ matrix.asset_name }}.tar.gz

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Display artifacts
        run: ls -la artifacts/*/

      - name: Upload release assets
        uses: softprops/action-gh-release@v1
        with:
          files: artifacts/*/*.tar.gz
          fail_on_unmatched_files: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
