name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: read

jobs:
  # =============================================================================
  # Build Jobs
  # =============================================================================

  build-macos:
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@fa04a1451ff1842e2626ccb99004d0195b455a88 # stable
        with:
          toolchain: stable

      - name: Add targets
        run: |
          rustup target add aarch64-apple-darwin
          rustup target add x86_64-apple-darwin

      - name: Build arm64
        run: cargo build --release --target aarch64-apple-darwin

      - name: Build x86_64
        run: cargo build --release --target x86_64-apple-darwin

      - name: Create universal binary
        run: |
          mkdir -p release
          lipo -create \
            target/aarch64-apple-darwin/release/baken \
            target/x86_64-apple-darwin/release/baken \
            -output release/baken

      - name: Get version
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Create tarball
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
        run: |
          cd release
          tar -czvf baken-${VERSION}-macos-universal.tar.gz baken

      - name: Calculate SHA256
        id: sha256
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
        run: |
          SHA256=$(shasum -a 256 release/baken-${VERSION}-macos-universal.tar.gz | awk '{print $1}')
          echo "SHA256=$SHA256" >> $GITHUB_OUTPUT

      - name: Upload artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: macos-universal
          path: release/baken-${{ steps.version.outputs.VERSION }}-macos-universal.tar.gz

      - name: Save SHA256
        env:
          SHA256: ${{ steps.sha256.outputs.SHA256 }}
        run: echo "$SHA256" > sha256-macos-universal.txt

      - name: Upload SHA256
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: sha256-macos-universal
          path: sha256-macos-universal.txt

  build-linux-x86_64:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@fa04a1451ff1842e2626ccb99004d0195b455a88 # stable
        with:
          toolchain: stable

      - name: Build
        run: cargo build --release

      - name: Get version
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Create tarball
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
        run: |
          mkdir -p release
          cp target/release/baken release/
          cd release
          tar -czvf baken-${VERSION}-linux-x86_64.tar.gz baken

      - name: Calculate SHA256
        id: sha256
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
        run: |
          SHA256=$(sha256sum release/baken-${VERSION}-linux-x86_64.tar.gz | awk '{print $1}')
          echo "SHA256=$SHA256" >> $GITHUB_OUTPUT

      - name: Upload artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: linux-x86_64
          path: release/baken-${{ steps.version.outputs.VERSION }}-linux-x86_64.tar.gz

      - name: Save SHA256
        env:
          SHA256: ${{ steps.sha256.outputs.SHA256 }}
        run: echo "$SHA256" > sha256-linux-x86_64.txt

      - name: Upload SHA256
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: sha256-linux-x86_64
          path: sha256-linux-x86_64.txt

  build-linux-aarch64:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@fa04a1451ff1842e2626ccb99004d0195b455a88 # stable
        with:
          toolchain: stable

      - name: Install cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build with cross
        run: cross build --release --target aarch64-unknown-linux-gnu

      - name: Get version
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Create tarball
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
        run: |
          mkdir -p release
          cp target/aarch64-unknown-linux-gnu/release/baken release/
          cd release
          tar -czvf baken-${VERSION}-linux-aarch64.tar.gz baken

      - name: Calculate SHA256
        id: sha256
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
        run: |
          SHA256=$(sha256sum release/baken-${VERSION}-linux-aarch64.tar.gz | awk '{print $1}')
          echo "SHA256=$SHA256" >> $GITHUB_OUTPUT

      - name: Upload artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: linux-aarch64
          path: release/baken-${{ steps.version.outputs.VERSION }}-linux-aarch64.tar.gz

      - name: Save SHA256
        env:
          SHA256: ${{ steps.sha256.outputs.SHA256 }}
        run: echo "$SHA256" > sha256-linux-aarch64.txt

      - name: Upload SHA256
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: sha256-linux-aarch64
          path: sha256-linux-aarch64.txt

  build-windows:
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@fa04a1451ff1842e2626ccb99004d0195b455a88 # stable
        with:
          toolchain: stable

      - name: Build
        run: cargo build --release

      - name: Get version
        id: version
        shell: bash
        run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT

      - name: Create zip
        shell: pwsh
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
        run: |
          New-Item -ItemType Directory -Force -Path release
          Copy-Item target\release\baken.exe release\
          Compress-Archive -Path release\baken.exe -DestinationPath release\baken-${env:VERSION}-windows-x86_64.zip

      - name: Calculate SHA256
        id: sha256
        shell: pwsh
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
        run: |
          $hash = (Get-FileHash release\baken-${env:VERSION}-windows-x86_64.zip -Algorithm SHA256).Hash.ToLower()
          echo "SHA256=$hash" >> $env:GITHUB_OUTPUT

      - name: Upload artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: windows-x86_64
          path: release/baken-${{ steps.version.outputs.VERSION }}-windows-x86_64.zip

      - name: Save SHA256
        shell: bash
        env:
          SHA256: ${{ steps.sha256.outputs.SHA256 }}
        run: echo "$SHA256" > sha256-windows-x86_64.txt

      - name: Upload SHA256
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: sha256-windows-x86_64
          path: sha256-windows-x86_64.txt

  # =============================================================================
  # Release Job - Create GitHub Release
  # =============================================================================

  release:
    needs: [build-macos, build-linux-x86_64, build-linux-aarch64, build-windows]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      version: ${{ steps.version.outputs.VERSION }}
      version_num: ${{ steps.version.outputs.VERSION_NUM }}
      macos_sha256: ${{ steps.macos_sha256.outputs.SHA256 }}
      windows_sha256: ${{ steps.windows_sha256.outputs.SHA256 }}
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v4

      - name: Get version
        id: version
        run: |
          VERSION="${GITHUB_REF#refs/tags/}"
          echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
          echo "VERSION_NUM=${VERSION#v}" >> $GITHUB_OUTPUT

      - name: Download all artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          path: artifacts

      - name: Prepare release files
        run: |
          mkdir -p release
          mv artifacts/macos-universal/*.tar.gz release/
          mv artifacts/linux-x86_64/*.tar.gz release/
          mv artifacts/linux-aarch64/*.tar.gz release/
          mv artifacts/windows-x86_64/*.zip release/

      - name: Create checksums file
        env:
          VERSION: ${{ steps.version.outputs.VERSION }}
        run: |
          cd release
          cat ../artifacts/sha256-macos-universal/sha256-macos-universal.txt | while read hash; do echo "$hash  baken-${VERSION}-macos-universal.tar.gz"; done > checksums.txt
          cat ../artifacts/sha256-linux-x86_64/sha256-linux-x86_64.txt | while read hash; do echo "$hash  baken-${VERSION}-linux-x86_64.tar.gz"; done >> checksums.txt
          cat ../artifacts/sha256-linux-aarch64/sha256-linux-aarch64.txt | while read hash; do echo "$hash  baken-${VERSION}-linux-aarch64.tar.gz"; done >> checksums.txt
          cat ../artifacts/sha256-windows-x86_64/sha256-windows-x86_64.txt | while read hash; do echo "$hash  baken-${VERSION}-windows-x86_64.zip"; done >> checksums.txt
          mv checksums.txt baken-${VERSION}-checksums.txt

      - name: Get macOS SHA256
        id: macos_sha256
        run: |
          SHA256=$(cat artifacts/sha256-macos-universal/sha256-macos-universal.txt)
          echo "SHA256=$SHA256" >> $GITHUB_OUTPUT

      - name: Get Windows SHA256
        id: windows_sha256
        run: |
          SHA256=$(cat artifacts/sha256-windows-x86_64/sha256-windows-x86_64.txt)
          echo "SHA256=$SHA256" >> $GITHUB_OUTPUT

      - name: Check for release notes
        id: release_notes
        run: |
          if [ -f "RELEASE_NOTES.md" ]; then
            echo "exists=true" >> $GITHUB_OUTPUT
          else
            echo "exists=false" >> $GITHUB_OUTPUT
          fi

      - name: Create Release (with custom notes)
        if: steps.release_notes.outputs.exists == 'true'
        uses: softprops/action-gh-release@718ea10b132b3b2eba29c1007bb80653f286566b # v2
        with:
          files: |
            release/baken-${{ steps.version.outputs.VERSION }}-macos-universal.tar.gz
            release/baken-${{ steps.version.outputs.VERSION }}-linux-x86_64.tar.gz
            release/baken-${{ steps.version.outputs.VERSION }}-linux-aarch64.tar.gz
            release/baken-${{ steps.version.outputs.VERSION }}-windows-x86_64.zip
            release/baken-${{ steps.version.outputs.VERSION }}-checksums.txt
          body_path: RELEASE_NOTES.md
          append_body: true

      - name: Create Release (auto-generated notes)
        if: steps.release_notes.outputs.exists == 'false'
        uses: softprops/action-gh-release@718ea10b132b3b2eba29c1007bb80653f286566b # v2
        with:
          files: |
            release/baken-${{ steps.version.outputs.VERSION }}-macos-universal.tar.gz
            release/baken-${{ steps.version.outputs.VERSION }}-linux-x86_64.tar.gz
            release/baken-${{ steps.version.outputs.VERSION }}-linux-aarch64.tar.gz
            release/baken-${{ steps.version.outputs.VERSION }}-windows-x86_64.zip
            release/baken-${{ steps.version.outputs.VERSION }}-checksums.txt
          generate_release_notes: true
          body: |
            ## Installation

            | Platform | Command |
            |----------|---------|
            | **macOS (Homebrew)** | `brew install M-Igashi/tap/baken` |
            | **Windows (winget)** | `winget install M-Igashi.baken` |
            | **Cargo** | `cargo install baken` (ffmpeg must be installed separately) |

            Package managers (Homebrew, winget) install ffmpeg automatically. mp3rgain is built-in — no separate installation needed.

            ## Checksums
            See `baken-${{ steps.version.outputs.VERSION }}-checksums.txt` for SHA256 checksums of all binaries.

  # =============================================================================
  # Publish to Cargo (crates.io)
  # =============================================================================

  publish-cargo:
    needs: [release]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@fa04a1451ff1842e2626ccb99004d0195b455a88 # stable
        with:
          toolchain: stable

      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --token $CARGO_REGISTRY_TOKEN

  # =============================================================================
  # Update Homebrew (macOS)
  # =============================================================================

  update-homebrew:
    needs: [release]
    runs-on: ubuntu-latest
    steps:
      - name: Update Homebrew formula
        env:
          HOMEBREW_TAP_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }}
          VERSION: ${{ needs.release.outputs.version }}
          VERSION_NUM: ${{ needs.release.outputs.version_num }}
          SHA256: ${{ needs.release.outputs.macos_sha256 }}
        run: |

          git clone https://x-access-token:${HOMEBREW_TAP_TOKEN}@github.com/M-Igashi/homebrew-tap.git
          cd homebrew-tap

          cat > Formula/baken.rb << 'FORMULA'
          class Baken < Formula
            desc "Audio loudness analyzer and gain adjustment tool for mastering and DJ workflows"
            homepage "https://github.com/M-Igashi/baken"
            url "https://github.com/M-Igashi/baken/releases/download/VERSION_PLACEHOLDER/baken-VERSION_PLACEHOLDER-macos-universal.tar.gz"
            sha256 "SHA256_PLACEHOLDER"
            version "VERSION_NUM_PLACEHOLDER"
            license "MIT"

            depends_on "ffmpeg"

            def install
              bin.install "baken"
            end

            def caveats
              <<~EOS
                mp3rgain is now built-in as a library dependency.
                No separate installation required for lossless MP3 gain adjustment.
              EOS
            end

            test do
              assert_match "baken", shell_output("#{bin}/baken --version")
            end
          end
          FORMULA

          sed -i "s|VERSION_PLACEHOLDER|${VERSION}|g" Formula/baken.rb
          sed -i "s|SHA256_PLACEHOLDER|${SHA256}|g" Formula/baken.rb
          sed -i "s|VERSION_NUM_PLACEHOLDER|${VERSION_NUM}|g" Formula/baken.rb

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