name: Nightly Release

on:
  push:
    branches:
      - master
  workflow_dispatch:

permissions:
  contents: write

jobs:
  nightly:
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version: "1.26.1"

      - name: Set up Zig
        uses: goto-bus-stop/setup-zig@v2

      - name: Get macOS SDK path
        id: macos_sdk
        run: echo "path=$(xcrun --show-sdk-path)" >> $GITHUB_OUTPUT

      - name: Build with GoReleaser (snapshot)
        uses: goreleaser/goreleaser-action@v7
        with:
          version: latest
          args: release --snapshot --clean --config .goreleaser.nightly.yml
        env:
          SDK_PATH: ${{ steps.macos_sdk.outputs.path }}

      - name: Delete existing nightly release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release delete nightly --yes --cleanup-tag || true

      - name: Create nightly release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create nightly dist/*.tar.gz dist/*.zip dist/checksums.txt \
            --title "Nightly Build ($(git rev-parse --short HEAD))" \
            --notes "This is an automated nightly build from the latest \`master\` commit ($(git rev-parse --short HEAD)).\nIt may be unstable — use stable releases for production." \
            --prerelease \
            --target master
