name: Release

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'

permissions:
  contents: write

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  build:
    name: Build ${{ matrix.target }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # Linux x86_64 (glibc)
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz

          # macOS (Intel)
          - target: x86_64-apple-darwin
            os: macos-latest
            archive: tar.gz

          # macOS (Apple Silicon)
          - target: aarch64-apple-darwin
            os: macos-latest
            archive: tar.gz

          # Windows x86_64 (GNU, cross-compiled via MinGW)
          - target: x86_64-pc-windows-gnu
            os: ubuntu-latest
            archive: zip
            apt: gcc-mingw-w64-x86-64
            linker: x86_64-w64-mingw32-gcc

    steps:
      - uses: actions/checkout@v6

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

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

      - name: Install system deps
        if: matrix.apt != ''
        run: sudo apt-get install -y ${{ matrix.apt }}

      - name: Set cross-linker
        if: matrix.linker != ''
        run: |
          mkdir -p .cargo
          printf '[target.${{ matrix.target }}]\nlinker = "${{ matrix.linker }}"\n' >> .cargo/config.toml

      - name: Build release binary
        run: cargo build --release --target ${{ matrix.target }}

      # ── Package ──────────────────────────────────────────────────────────
      - name: Package (tar.gz)
        if: matrix.archive == 'tar.gz'
        shell: bash
        run: |
          BIN=target/${{ matrix.target }}/release/ecotokens
          ASSET=ecotokens-${{ github.ref_name }}-${{ matrix.target }}.tar.gz
          tar czf "$ASSET" -C "$(dirname "$BIN")" "$(basename "$BIN")"
          echo "ASSET=$ASSET" >> "$GITHUB_ENV"

      - name: Package (zip)
        if: matrix.archive == 'zip'
        shell: bash
        run: |
          BIN=target/${{ matrix.target }}/release/ecotokens.exe
          ASSET=ecotokens-${{ github.ref_name }}-${{ matrix.target }}.zip
          zip -j "$ASSET" "$BIN"
          echo "ASSET=$ASSET" >> "$GITHUB_ENV"

      - name: Upload artifact
        uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.target }}
          path: ${{ env.ASSET }}

  # ── Create GitHub Release and attach all assets ──────────────────────────
  release:
    name: Publish release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v8
        with:
          path: artifacts
          merge-multiple: true

      - name: Create release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release delete "${{ github.ref_name }}" --repo "${{ github.repository }}" --yes 2>/dev/null || true
          gh release create "${{ github.ref_name }}" \
            --repo "${{ github.repository }}" \
            --generate-notes \
            artifacts/*
