name: Release

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

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  verify-version:
    name: Verify Tag Matches Cargo.toml
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Compare tag with package version
        run: ./scripts/check-version-match.sh

  build:
    name: Build ${{ matrix.target }}
    needs: verify-version
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-latest
            target: aarch64-apple-darwin
          - os: macos-latest
            target: x86_64-apple-darwin
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            cross: true
          - os: windows-latest
            target: x86_64-pc-windows-msvc

    steps:
      - uses: actions/checkout@v5

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

      - name: Install cross (for cross-compilation)
        if: matrix.cross
        run: cargo install cross --git https://github.com/cross-rs/cross

      - name: Build (native)
        if: "!matrix.cross"
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build (cross)
        if: matrix.cross
        run: cross build --release --target ${{ matrix.target }}

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: |
          BINARY_NAME="auditmysite"
          TARGET="${{ matrix.target }}"
          VERSION="${GITHUB_REF_NAME}"
          ARCHIVE_NAME="${BINARY_NAME}-${VERSION}-${TARGET}.tar.gz"

          cp "target/${TARGET}/release/${BINARY_NAME}" .
          tar -czf "${ARCHIVE_NAME}" "${BINARY_NAME}"
          echo "ARCHIVE_NAME=${ARCHIVE_NAME}" >> $GITHUB_ENV

      - name: Package (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          $BINARY_NAME = "auditmysite.exe"
          $TARGET = "${{ matrix.target }}"
          $VERSION = "$env:GITHUB_REF_NAME"
          $ARCHIVE_NAME = "auditmysite-${VERSION}-${TARGET}.zip"

          Copy-Item "target/${TARGET}/release/${BINARY_NAME}" .
          Compress-Archive -Path $BINARY_NAME -DestinationPath $ARCHIVE_NAME
          echo "ARCHIVE_NAME=${ARCHIVE_NAME}" >> $env:GITHUB_ENV

      - name: Generate SHA256 checksum (Unix)
        if: runner.os != 'Windows'
        run: shasum -a 256 "${{ env.ARCHIVE_NAME }}" > "${{ env.ARCHIVE_NAME }}.sha256"

      - name: Generate SHA256 checksum (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          $hash = (Get-FileHash "${{ env.ARCHIVE_NAME }}" -Algorithm SHA256).Hash.ToLower()
          "$hash  ${{ env.ARCHIVE_NAME }}" | Out-File -Encoding ascii "${{ env.ARCHIVE_NAME }}.sha256"

      - name: Upload artifact
        uses: actions/upload-artifact@v6
        with:
          name: binary-${{ matrix.target }}
          path: |
            ${{ env.ARCHIVE_NAME }}
            ${{ env.ARCHIVE_NAME }}.sha256
          retention-days: 1

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - name: Download all artifacts
        uses: actions/download-artifact@v5
        with:
          path: artifacts
          merge-multiple: true

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2.3.3
        with:
          generate_release_notes: true
          files: artifacts/*
