name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write
  id-token: write

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo test --all-features

  build:
    needs: test
    strategy:
      matrix:
        include:
          - target: aarch64-apple-darwin
            os: macos-14
            npm_pkg: cli-agents-darwin-arm64
            binary: cli-agents
          - target: x86_64-apple-darwin
            os: macos-15
            npm_pkg: cli-agents-darwin-x64
            binary: cli-agents
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            npm_pkg: cli-agents-linux-x64
            binary: cli-agents
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            npm_pkg: cli-agents-linux-arm64
            binary: cli-agents
            cross: true
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            npm_pkg: cli-agents-win32-x64
            binary: cli-agents.exe
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: Install cross-compilation tools
        if: matrix.cross
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu
          echo 'CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc' >> $GITHUB_ENV
      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}
      - run: cargo build --release --features cli --target ${{ matrix.target }}
      - name: Stage binary
        shell: bash
        run: |
          mkdir -p staging
          cp target/${{ matrix.target }}/release/${{ matrix.binary }} staging/
      - uses: actions/upload-artifact@v4
        with:
          name: binary-${{ matrix.npm_pkg }}
          path: staging/

  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    environment: npm
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 22
          registry-url: https://registry.npmjs.org
      - run: npm install -g npm@latest
      - uses: actions/download-artifact@v4
        with:
          path: artifacts/
      - name: Place binaries into platform packages
        run: |
          for pkg in darwin-arm64 darwin-x64 linux-x64 linux-arm64 win32-x64; do
            mkdir -p npm/cli-agents-${pkg}/bin
            cp artifacts/binary-cli-agents-${pkg}/* npm/cli-agents-${pkg}/bin/
            chmod +x npm/cli-agents-${pkg}/bin/* || true
          done
      - name: Set versions from tag
        run: |
          VERSION=${GITHUB_REF#refs/tags/v}
          for dir in npm/cli-agents npm/cli-agents-*/; do
            node -e "
              const fs = require('fs');
              const pkg = JSON.parse(fs.readFileSync('./${dir}/package.json', 'utf8'));
              pkg.version = '${VERSION}';
              if (pkg.optionalDependencies) {
                for (const k of Object.keys(pkg.optionalDependencies)) {
                  pkg.optionalDependencies[k] = '${VERSION}';
                }
              }
              fs.writeFileSync('./${dir}/package.json', JSON.stringify(pkg, null, 2) + '\n');
            "
          done
      - name: Publish platform packages
        run: |
          for pkg in cli-agents-darwin-arm64 cli-agents-darwin-x64 cli-agents-linux-x64 cli-agents-linux-arm64 cli-agents-win32-x64; do
            (cd npm/${pkg} && npm publish --access public --provenance) || true
          done
      - name: Publish main package
        run: cd npm/cli-agents && npm publish --access public --provenance

  github-release:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/download-artifact@v4
        with:
          path: artifacts/
      - name: Package binaries
        run: |
          mkdir -p release
          for pkg in darwin-arm64 darwin-x64 linux-x64 linux-arm64; do
            tar czf release/cli-agents-${pkg}.tar.gz -C artifacts/binary-cli-agents-${pkg} cli-agents
          done
          cd artifacts/binary-cli-agents-win32-x64 && zip ../../release/cli-agents-win32-x64.zip cli-agents.exe
      - name: Create GitHub Release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release create ${{ github.ref_name }} release/* \
            --repo ${{ github.repository }} \
            --title "${{ github.ref_name }}" \
            --generate-notes

  publish-crate:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo publish || true
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
