name: Pre-Release

on:
  push:
    branches: [master]
    paths-ignore:
      - "www/**"
      - "docs/**"
      - ".github/workflows/docs.yml"
      - "*.md"
      - "LICENSE"
  workflow_dispatch:

permissions:
  contents: write
  actions: read

concurrency:
  group: prerelease-${{ github.ref }}
  cancel-in-progress: true

env:
  PRERELEASE_TAG: v0.0.0-prerelease

jobs:
  goreleaser:
    # macOS runner (Apple Silicon) builds arm64 extras + cross-compiles linux/windows via Zig
    runs-on: macos-latest
    if: github.ref_type == 'branch' && github.ref == 'refs/heads/master'

    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v7
        with:
          go-version: "1.26"
          cache: true

      # - name: Install Rosetta (for x86_64 cross-build)
      #   run: softwareupdate --install-rosetta --agree-to-license || true
      - name: Install dependencies (arm64)
        run: |
          brew install --quiet pkg-config zig
        env:
          HOMEBREW_NO_AUTO_UPDATE: 1
        # x86_64 homebrew (Rosetta) bootstrap kept for later use; uncomment if/when Intel builds are needed.
        # run: |
        #   softwareupdate --install-rosetta --agree-to-license || true
        #   mkdir /tmp/homebrew
        #   cd /tmp; curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew
        #   sudo mv /tmp/homebrew /usr/local/homebrew
        #   arch -x86_64 /usr/local/homebrew/bin/brew install --quiet unicorn libusb pkg-config
        # env:
        #   HOMEBREW_NO_AUTO_UPDATE: 1

      - name: Delete existing prerelease/tag
        run: gh release delete "$PRERELEASE_TAG" -y --cleanup-tag || true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Retag prerelease to current commit
        run: |
          git tag -f "$PRERELEASE_TAG" "$(git rev-parse HEAD)"

      - name: Run GoReleaser (prerelease)
        uses: goreleaser/goreleaser-action@v7
        with:
          distribution: goreleaser
          version: "~> v2"
          args: >
            release --clean --skip=announce
            --config hack/goreleaser/prerelease.yml
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GORELEASER_CURRENT_TAG: ${{ env.PRERELEASE_TAG }}

      - name: Upload dist artifacts (for debugging)
        if: failure()
        uses: actions/upload-artifact@v7
        with:
          name: prerelease-dist
          path: dist/**
