name: Nightly build
on:
  schedule:
    - cron: "0 0 * * *"
  workflow_dispatch:

jobs:
  linux:
    name: Linux build
    runs-on: ubuntu-latest
    strategy:
      matrix:
        features:
          - ["stateless"]
          - ["stateless", "parallel"]
          - ["pmtree-ft"]
          - ["pmtree-ft", "parallel"]
          - ["fullmerkletree"]
          - ["fullmerkletree", "parallel"]
          - ["optimalmerkletree"]
          - ["optimalmerkletree", "parallel"]
        target: [x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu]
    env:
      FEATURES_CARGO: ${{ join(matrix.features, ',') }}
      FEATURES_TAG: ${{ join(matrix.features, '-') }}
      TARGET: ${{ matrix.target }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          target: ${{ env.TARGET }}
      - uses: Swatinem/rust-cache@v2
      - name: Install dependencies
        run: make installdeps
      - name: Cross build
        run: |
          cross build --release --target $TARGET --no-default-features --features "$FEATURES_CARGO" --workspace
          mkdir release
          cp target/$TARGET/release/librln* release/
          tar -czvf $TARGET-$FEATURES_TAG-rln.tar.gz release/
      - name: Upload archive artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.TARGET }}-${{ env.FEATURES_TAG }}-archive
          path: ${{ env.TARGET }}-${{ env.FEATURES_TAG }}-rln.tar.gz
          retention-days: 2

  macos:
    name: MacOS build
    runs-on: macos-latest
    strategy:
      matrix:
        features:
          - ["stateless"]
          - ["stateless", "parallel"]
          - ["pmtree-ft"]
          - ["pmtree-ft", "parallel"]
          - ["fullmerkletree"]
          - ["fullmerkletree", "parallel"]
          - ["optimalmerkletree"]
          - ["optimalmerkletree", "parallel"]
        target: [x86_64-apple-darwin, aarch64-apple-darwin]
    env:
      FEATURES_CARGO: ${{ join(matrix.features, ',') }}
      FEATURES_TAG: ${{ join(matrix.features, '-') }}
      TARGET: ${{ matrix.target }}
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          target: ${{ env.TARGET }}
      - uses: Swatinem/rust-cache@v2
      - name: Install dependencies
        run: make installdeps
      - name: Cross build
        run: |
          cross build --release --target $TARGET --no-default-features --features "$FEATURES_CARGO" --workspace
          mkdir release
          cp target/$TARGET/release/librln* release/
          tar -czvf $TARGET-$FEATURES_TAG-rln.tar.gz release/
      - name: Upload archive artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.TARGET }}-${{ env.FEATURES_TAG }}-archive
          path: ${{ env.TARGET }}-${{ env.FEATURES_TAG }}-rln.tar.gz
          retention-days: 2

  rln-wasm:
    name: Build rln-wasm
    runs-on: ubuntu-latest
    strategy:
      matrix:
        feature:
          - "default"
          - "parallel"
          - "utils"
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
      - name: Install stable toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: wasm32-unknown-unknown
      - name: Install nightly toolchain
        uses: dtolnay/rust-toolchain@nightly
        with:
          components: rust-src
          targets: wasm32-unknown-unknown
      - uses: Swatinem/rust-cache@v2
      - name: Install dependencies
        run: make installdeps
      - name: Build rln-wasm package
        run: |
          if [[ ${{ matrix.feature }} == "parallel" ]]; then
            env CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUSTFLAGS="-C target-feature=+atomics,+bulk-memory,+mutable-globals -C link-arg=--shared-memory -C link-arg=--max-memory=1073741824 -C link-arg=--import-memory -C link-arg=--export=__wasm_init_tls -C link-arg=--export=__tls_size -C link-arg=--export=__tls_align -C link-arg=--export=__tls_base" \
            rustup run nightly wasm-pack build --release --target web --scope waku \
            --features parallel -Z build-std=panic_abort,std
            sed -i.bak 's/rln-wasm/zerokit-rln-wasm-parallel/g' pkg/package.json && rm pkg/package.json.bak
          elif [[ ${{ matrix.feature }} == "utils" ]]; then
            wasm-pack build --release --target web --scope waku --no-default-features --features utils
            sed -i.bak 's/rln-wasm/zerokit-rln-wasm-utils/g' pkg/package.json && rm pkg/package.json.bak
          else
            wasm-pack build --release --target web --scope waku
            sed -i.bak 's/rln-wasm/zerokit-rln-wasm/g' pkg/package.json && rm pkg/package.json.bak
          fi

          jq '. + {keywords: ["zerokit", "rln", "wasm"]}' pkg/package.json > pkg/package.json.tmp && \
          mv pkg/package.json.tmp pkg/package.json

          mkdir release
          cp -r pkg/* release/
          tar -czvf rln-wasm-${{ matrix.feature }}.tar.gz release/
        working-directory: rln-wasm
      - name: Upload archive artifact
        uses: actions/upload-artifact@v4
        with:
          name: rln-wasm-${{ matrix.feature }}-archive
          path: rln-wasm/rln-wasm-${{ matrix.feature }}.tar.gz
          retention-days: 2

  prepare-prerelease:
    name: Prepare pre-release
    needs: [linux, macos, rln-wasm]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: master
      - name: Download artifacts
        uses: actions/download-artifact@v4
      - name: Delete tag
        uses: dev-drprasad/delete-tag-and-release@v0.2.1
        with:
          delete_release: true
          tag_name: nightly
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Create prerelease
        run: |
          start_tag=$(gh release list -L 2 --exclude-drafts | grep -v nightly | cut -d$'\t' -f3 | sed -n '1p')
          gh release create nightly --prerelease --target master \
            --title 'Nightly build ("master" branch)' \
            --generate-notes \
            --draft=false \
            --notes-start-tag $start_tag \
            *-archive/*.tar.gz \
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Delete artifacts
        uses: geekyeggo/delete-artifact@v5
        with:
          failOnError: false
          name: |
            *-archive
