name: Publish

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: write

jobs:
  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Check if version is already published
        id: check_version
        run: |
          VERSION=$(grep '^version =' Cargo.toml | cut -d '"' -f 2)
          echo "Checking if load-rs version $VERSION is already on crates.io..."
          if curl -sL -H "User-Agent: load-rs-ci (github.com/fredyw/load-rs)" https://crates.io/api/v1/crates/load-rs | grep -q "\"num\":\"$VERSION\""; then
            echo "Version $VERSION already exists on crates.io. Skipping publish step."
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "Version $VERSION not found on crates.io. Proceeding with publish."
            echo "exists=false" >> $GITHUB_OUTPUT
          fi

      - name: Publish
        if: steps.check_version.outputs.exists != 'true'
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  build:
    name: Build Binaries
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact_name: load-rs-linux-x86_64
            binary_name: load-rs
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-gnu
            artifact_name: load-rs-linux-arm64
            binary_name: load-rs
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: load-rs-macos-arm64
            binary_name: load-rs
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_name: load-rs-windows-x86_64.exe
            binary_name: load-rs.exe
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}
      
      - name: Prepare binary
        shell: bash
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/${{ matrix.binary_name }} dist/${{ matrix.artifact_name }}

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

  release:
    name: Create GitHub Release
    needs: [publish, build]
    if: always() && needs.publish.result != 'cancelled' && needs.build.result == 'success'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Create Release and Upload Assets
        uses: softprops/action-gh-release@v2
        with:
          files: dist/*
          generate_release_notes: true
          make_latest: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
