name: Release

on:
  push:
    tags: ["v*"]

env:
  CARGO_TERM_COLOR: always

jobs:
  # Gate: run the full CI suite before building release artifacts.
  # Reuses the CI workflow so we don't duplicate check/fmt/clippy/test/doc.
  ci:
    name: CI
    uses: ./.github/workflows/ci.yml

  build:
    name: Build ${{ matrix.target }}
    needs: ci
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cross-compilation tools
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu
          echo "CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER=aarch64-linux-gnu-gcc" >> $GITHUB_ENV

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

      - name: Package (unix)
        if: runner.os != 'Windows'
        run: |
          mkdir hunch-${{ matrix.target }}
          cp target/${{ matrix.target }}/release/hunch hunch-${{ matrix.target }}/
          cp README.md LICENSE hunch-${{ matrix.target }}/
          tar czf hunch-${{ matrix.target }}.tar.gz hunch-${{ matrix.target }}

      - name: Package (windows)
        if: runner.os == 'Windows'
        shell: bash
        run: |
          mkdir hunch-${{ matrix.target }}
          cp target/${{ matrix.target }}/release/hunch.exe hunch-${{ matrix.target }}/
          cp README.md LICENSE hunch-${{ matrix.target }}/
          tar czf hunch-${{ matrix.target }}.tar.gz hunch-${{ matrix.target }}

      - uses: actions/upload-artifact@v4
        with:
          name: hunch-${{ matrix.target }}
          path: hunch-${{ matrix.target }}.tar.gz

  publish:
    name: Publish to crates.io
    needs: build
    runs-on: ubuntu-latest
    environment: crates-io
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  github-release:
    name: GitHub Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - uses: actions/download-artifact@v4
        with:
          path: artifacts
          merge-multiple: true

      - name: Extract release notes
        id: notes
        run: |
          TAG="${GITHUB_REF#refs/tags/v}"
          if [ -f RELEASE_NOTES.md ] && [ -s RELEASE_NOTES.md ]; then
            cp RELEASE_NOTES.md release_notes.md
            echo "generate=false" >> $GITHUB_OUTPUT
          else
            awk "/^## \\[${TAG}\\]/{found=1; next} /^## \\[/{if(found) exit} found" CHANGELOG.md > release_notes.md
            if [ ! -s release_notes.md ]; then
              echo "generate=true" >> $GITHUB_OUTPUT
            else
              echo "generate=false" >> $GITHUB_OUTPUT
            fi
          fi

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          generate_release_notes: ${{ steps.notes.outputs.generate == 'true' }}
          body_path: ${{ steps.notes.outputs.generate != 'true' && 'release_notes.md' || '' }}
          files: artifacts/*.tar.gz

  homebrew:
    name: Update Homebrew Tap
    needs: github-release
    runs-on: ubuntu-latest
    steps:
      - name: Wait for release assets to be available
        run: sleep 10

      - name: Download release tarballs and compute SHA256
        id: sha
        env:
          GH_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
        run: |
          VERSION="${GITHUB_REF#refs/tags/v}"
          echo "version=${VERSION}" >> $GITHUB_OUTPUT
          BASE="https://github.com/lijunzh/hunch/releases/download/v${VERSION}"

          for target in aarch64-apple-darwin x86_64-apple-darwin aarch64-unknown-linux-gnu x86_64-unknown-linux-gnu; do
            curl -fsSL -o "${target}.tar.gz" "${BASE}/hunch-${target}.tar.gz"
            sha=$(sha256sum "${target}.tar.gz" | awk '{print $1}')
            # Convert target to env-friendly name: aarch64-apple-darwin -> aarch64_apple_darwin
            key=$(echo "$target" | tr '-' '_')
            echo "${key}=${sha}" >> $GITHUB_OUTPUT
            echo "  ${target}: ${sha}"
          done

      - name: Update formula
        env:
          GH_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
        run: |
          VERSION="${{ steps.sha.outputs.version }}"
          git clone https://x-access-token:${GH_TOKEN}@github.com/lijunzh/homebrew-hunch.git
          cd homebrew-hunch

          cat > Formula/hunch.rb << 'FORMULA'
          class Hunch < Formula
            desc "Fast, offline media filename parser — extract metadata from messy filenames"
            homepage "https://github.com/lijunzh/hunch"
            version "__VERSION__"
            license "MIT"

            on_macos do
              if Hardware::CPU.arm?
                url "https://github.com/lijunzh/hunch/releases/download/v#{version}/hunch-aarch64-apple-darwin.tar.gz"
                sha256 "__SHA_ARM64_MACOS__"
              else
                url "https://github.com/lijunzh/hunch/releases/download/v#{version}/hunch-x86_64-apple-darwin.tar.gz"
                sha256 "__SHA_X86_64_MACOS__"
              end
            end

            on_linux do
              if Hardware::CPU.arm?
                url "https://github.com/lijunzh/hunch/releases/download/v#{version}/hunch-aarch64-unknown-linux-gnu.tar.gz"
                sha256 "__SHA_ARM64_LINUX__"
              else
                url "https://github.com/lijunzh/hunch/releases/download/v#{version}/hunch-x86_64-unknown-linux-gnu.tar.gz"
                sha256 "__SHA_X86_64_LINUX__"
              end
            end

            def install
              bin.install Dir["**/hunch"].first
            end

            test do
              output = shell_output("#{bin}/hunch 'The.Matrix.1999.1080p.BluRay.x264-GROUP.mkv'")
              assert_match '"title": "The Matrix"', output
              assert_match '"year": 1999', output
            end
          end
          FORMULA

          # Strip leading whitespace from heredoc
          sed -i 's/^          //' Formula/hunch.rb

          # Substitute placeholders
          sed -i "s/__VERSION__/${VERSION}/" Formula/hunch.rb
          sed -i "s/__SHA_ARM64_MACOS__/${{ steps.sha.outputs.aarch64_apple_darwin }}/" Formula/hunch.rb
          sed -i "s/__SHA_X86_64_MACOS__/${{ steps.sha.outputs.x86_64_apple_darwin }}/" Formula/hunch.rb
          sed -i "s/__SHA_ARM64_LINUX__/${{ steps.sha.outputs.aarch64_unknown_linux_gnu }}/" Formula/hunch.rb
          sed -i "s/__SHA_X86_64_LINUX__/${{ steps.sha.outputs.x86_64_unknown_linux_gnu }}/" Formula/hunch.rb

          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add Formula/hunch.rb
          git commit -m "hunch v${VERSION}"
          git push
