name: Release

on:
  workflow_dispatch:
    inputs:
      release_tag:
        description: 'Release tag (e.g. v0.1.0). Leave empty to only build artifacts (no release).'
        required: false
        type: string
      toolchain:
        description: 'Rust toolchain'
        required: true
        default: stable
        type: choice
        options:
          - stable
  push:
    tags:
      - 'v*'

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  build-release:
    name: Build Release (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            name: trx-linux-x86_64
          - target: x86_64-apple-darwin
            os: macos-latest
            name: trx-macos-x86_64
          - target: aarch64-apple-darwin
            os: macos-latest
            name: trx-macos-aarch64
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            name: trx-windows-x86_64

    steps:
      - uses: actions/checkout@v4
      
      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: ${{ github.event.inputs.toolchain || 'stable' }}
          targets: ${{ matrix.target }}
      
      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}
      
      - name: Build
        run: cargo build --release --target ${{ matrix.target }}
      
      - name: Prepare artifact (Unix)
        if: matrix.os != 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          strip trx || true
          tar -czvf ../../../${{ matrix.name }}.tar.gz trx
      
      - name: Prepare artifact (Windows)
        if: matrix.os == 'windows-latest'
        run: |
          cd target/${{ matrix.target }}/release
          Compress-Archive -Path trx.exe -DestinationPath ../../../${{ matrix.name }}.zip -Force
      
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.name }}
          path: |
            ${{ matrix.name }}.tar.gz
            ${{ matrix.name }}.zip
          if-no-files-found: ignore

  release:
    name: Create Release
    needs: build-release
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/') || (github.event_name == 'workflow_dispatch' && github.event.inputs.release_tag != '')
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ github.event.inputs.release_tag || github.ref_name }}
          name: trx ${{ github.event.inputs.release_tag || github.ref_name }}
          draft: false
          prerelease: ${{ contains(github.event.inputs.release_tag || github.ref_name, '-') }}
          generate_release_notes: true
          files: artifacts/*

  publish-crates-io:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/')
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: Publish to crates.io
        run: |
          if [ -z "${CARGO_REGISTRY_TOKEN}" ]; then
            echo "Error: CARGO_REGISTRY_TOKEN is empty. Check your GitHub Secrets."
            exit 1
          fi
          cargo publish --token "${CARGO_REGISTRY_TOKEN}" --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
