name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build-wasm:
    name: Build WASM frontend
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown

      - name: Install trunk
        uses: jetli/trunk-action@v0.5.0

      - name: Build WASM
        run: |
          cd web-ui
          trunk build --release

      - name: Stage WASM assets
        run: |
          rm -rf static/web && mkdir -p static/web
          cp -r web-ui/dist/* static/web/

      - name: Upload WASM assets
        uses: actions/upload-artifact@v4
        with:
          name: wasm-assets
          path: static/web/
          retention-days: 1

  build-unix:
    name: ${{ matrix.name }}
    needs: build-wasm
    runs-on: ${{ matrix.runner }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - name: macOS ARM
            runner: macos-26
            target: aarch64-apple-darwin
            artifact: repartee-macos-arm64
          - name: Linux AMD64
            runner: ubuntu-24.04
            target: x86_64-unknown-linux-gnu
            artifact: repartee-linux-amd64
            deps: sudo apt-get update && sudo apt-get install -y libchafa-dev libglib2.0-dev pkg-config
          - name: Linux ARM64
            runner: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            artifact: repartee-linux-arm64
            deps: sudo apt-get update && sudo apt-get install -y libchafa-dev libglib2.0-dev pkg-config
    steps:
      - uses: actions/checkout@v6

      - name: Download WASM assets
        uses: actions/download-artifact@v4
        with:
          name: wasm-assets
          path: static/web/

      - name: Install system dependencies (Linux)
        if: contains(matrix.runner, 'ubuntu')
        run: ${{ matrix.deps }}

      - name: Install system dependencies (macOS)
        if: contains(matrix.runner, 'macos')
        run: brew install chafa

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

      - name: Build release binary
        env:
          PKG_CONFIG_PATH: /usr/lib/x86_64-linux-gnu/pkgconfig:/usr/lib/aarch64-linux-gnu/pkgconfig
        run: cargo build --release --locked

      - name: Strip binary
        run: strip target/release/repartee

      - name: Package artifact
        run: |
          cd target/release
          tar czf ../../${{ matrix.artifact }}.tar.gz repartee
          cd ../..

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

  build-freebsd:
    name: FreeBSD AMD64
    needs: build-wasm
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6

      - name: Download WASM assets
        uses: actions/download-artifact@v4
        with:
          name: wasm-assets
          path: static/web/

      - name: Build in FreeBSD VM
        uses: vmactions/freebsd-vm@v1
        with:
          release: "14.2"
          usesh: true
          mem: 8192
          cpu: 4
          prepare: |
            pkg install -y rust chafa pkgconf
          run: |
            cargo build --release --locked
            strip target/release/repartee
            cd target/release
            tar czf ../../repartee-freebsd-amd64.tar.gz repartee

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: repartee-freebsd-amd64
          path: repartee-freebsd-amd64.tar.gz

  release:
    name: Create Release
    needs: [build-unix, build-freebsd]
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v6

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

      - name: List artifacts
        run: ls -la artifacts/

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: true
          files: artifacts/*.tar.gz