name: Release

on:
  push:
    tags: ["v*"]

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  # Fail fast if Cargo.toml and npm/package.json versions don't match the tag
  version-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Verify versions match tag
        run: |
          TAG="${GITHUB_REF#refs/tags/v}"
          CARGO_VER=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)"/\1/')
          NPM_VER=$(node -p "require('./npm/package.json').version")
          echo "Tag: $TAG  Cargo: $CARGO_VER  npm: $NPM_VER"
          if [ "$CARGO_VER" != "$TAG" ]; then
            echo "::error::Cargo.toml version ($CARGO_VER) does not match tag ($TAG)"
            exit 1
          fi
          if [ "$NPM_VER" != "$TAG" ]; then
            echo "::error::npm/package.json version ($NPM_VER) does not match tag ($TAG)"
            exit 1
          fi

  build:
    needs: version-check
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
            cross: true
          - target: x86_64-apple-darwin
            os: macos-latest
            cross: false
          - target: aarch64-apple-darwin
            os: macos-latest
            cross: false
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            cross: false

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4

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

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      # Install cross for Linux targets (cross-compiles via Docker)
      - name: Install cross
        if: matrix.cross
        run: cargo install cross --locked

      # Build
      - name: Build
        run: |
          if [ "${{ matrix.cross }}" = "true" ]; then
            cross build --release --locked --target ${{ matrix.target }}
          else
            cargo build --release --locked --target ${{ matrix.target }}
          fi
        shell: bash

      # Package — unix
      - name: Package (unix)
        if: matrix.os != 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          tar czf ../../../tilth-${{ matrix.target }}.tar.gz tilth
          cd ../../..

      # Package — windows
      - name: Package (windows)
        if: matrix.os == 'windows-latest'
        shell: bash
        run: |
          cd target/${{ matrix.target }}/release
          7z a ../../../tilth-${{ matrix.target }}.zip tilth.exe
          cd ../../..

      # Upload to GitHub release
      - uses: softprops/action-gh-release@v2
        with:
          files: |
            tilth-*.tar.gz
            tilth-*.zip
          generate_release_notes: true

  # After all builds, publish to crates.io
  publish-crate:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo publish --locked
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  # After all builds, publish to npm
  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: https://registry.npmjs.org
      - run: npm publish ./npm
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
