name: Release

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  build-and-release:
    name: Build and Release
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_name: indodax-linux-x86_64
            binary_extension: ""
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            artifact_name: indodax-linux-aarch64
            binary_extension: ""
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact_name: indodax-macos-x86_64
            binary_extension: ""
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: indodax-macos-aarch64
            binary_extension: ""
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_name: indodax-windows-x86_64
            binary_extension: ".exe"

    steps:
      - name: Checkout
        uses: actions/checkout@v4

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

      - name: Install cross-compilation tools (Linux ARM64)
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu

      - name: Build
        shell: bash
        run: |
          # Use cross for linux aarch64 if needed, but here we try native build with linker
          if [ "${{ matrix.target }}" == "aarch64-unknown-linux-gnu" ]; then
            export CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc
          fi
          
          cargo build --release --target ${{ matrix.target }}
          
          # Move and rename binary for easier release management
          BIN_DIR="target/${{ matrix.target }}/release"
          cp "$BIN_DIR/indodax${{ matrix.binary_extension }}" "${{ matrix.artifact_name }}${{ matrix.binary_extension }}"

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.artifact_name }}
          path: ${{ matrix.artifact_name }}${{ matrix.binary_extension }}

  publish-cargo:
    name: Publish to Cargo
    needs: build-and-release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      - name: Publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --token $CARGO_REGISTRY_TOKEN --no-verify --allow-dirty || echo "Already published"

  publish-npm:
    name: Publish to NPM
    needs: build-and-release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'
      - name: Install wasm-pack
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
      - name: Build paper-wasm
        run: cd paper-wasm && wasm-pack build --scope ibidathoillah
      - name: Publish paper-wasm
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          cd paper-wasm/pkg
          npm publish --access public || echo "Already published"
      - name: Publish indodax-cli
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: npm publish --access public || echo "Already published"

  github-release:
    name: GitHub Release
    needs: build-and-release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Download Artifacts
        uses: actions/download-artifact@v4
        with:
          # Download directly to the root for easier matching
          merge-multiple: true

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            indodax-linux-x86_64
            indodax-linux-aarch64
            indodax-macos-x86_64
            indodax-macos-aarch64
            indodax-windows-x86_64.exe
          body_path: RELEASE_NOTES.md
          fail_on_unmatched_files: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
