name: Release

# Release workflow is tag-driven: pushing an annotated tag of the form
# `vX.Y.Z` (matching the `Cargo.toml` version bump) fires all three legs
# in parallel — crates.io publish, GitHub Release binaries, and a fresh
# VHS demo. A missing `CARGO_REGISTRY_TOKEN` secret fails `cargo-publish`
# loudly so a partial release is obvious; `GITHUB_TOKEN` is provided
# automatically and is enough for the binary + asset uploads.
on:
  push:
    tags:
      - 'v*.*.*'

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  # -----------------------------------------------------------------
  # Publish to crates.io. `--locked` pins the lockfile so the
  # published crate matches the binary releases bit-for-bit.
  # Fails fast on version collisions or missing token — preferred
  # over silently skipping so the maintainer notices.
  # -----------------------------------------------------------------
  cargo-publish:
    name: cargo publish
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain (stable)
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry & target
        uses: Swatinem/rust-cache@v2

      - name: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish --locked

  # -----------------------------------------------------------------
  # Create the GitHub Release once, from a single job, before the
  # binary matrix fans out. `taiki-e/upload-rust-binary-action` does
  # not reliably handle three matrix jobs racing to create the same
  # release — they each saw "release not found" on v0.3.1. Doing it
  # here first removes the race and gives us a place to hang release
  # notes off in the future.
  # -----------------------------------------------------------------
  create-release:
    name: create github release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: taiki-e/create-gh-release-action@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  # -----------------------------------------------------------------
  # Build release binaries for the supported targets and upload
  # each as an archive to the GitHub Release created above. Windows
  # is intentionally omitted: kizu is a Unix TUI (crossterm + notify +
  # raw tmux workflow).
  # -----------------------------------------------------------------
  release-binaries:
    name: binary / ${{ matrix.target }}
    needs: create-release
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-apple-darwin
            os: macos-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4

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

      - name: Cache cargo registry & target
        uses: Swatinem/rust-cache@v2

      - name: Build + upload kizu binary
        uses: taiki-e/upload-rust-binary-action@v1
        with:
          bin: kizu
          target: ${{ matrix.target }}
          archive: $bin-$tag-$target
          token: ${{ secrets.GITHUB_TOKEN }}
          checksum: sha256

  # -----------------------------------------------------------------
  # Re-record the VHS demo against the freshly built release
  # binary and attach the resulting GIF to the GitHub Release so
  # the release notes have a current demo without waiting for the
  # maintainer to run `just demo` locally. The in-repo
  # `docs/media/demo.gif` referenced from README is refreshed
  # separately via `demo-refresh.yml` to keep the release path
  # free of PR-opening side effects.
  # -----------------------------------------------------------------
  release-vhs:
    name: vhs / re-record demo
    runs-on: ubuntu-latest
    needs: release-binaries
    steps:
      - uses: actions/checkout@v4

      - name: Install Rust toolchain (stable)
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo registry & target
        uses: Swatinem/rust-cache@v2

      - name: Install tmux (vhs tape dependency)
        run: sudo apt-get update && sudo apt-get install -y tmux

      - name: Install kizu on PATH
        run: cargo install --path . --locked

      - name: Record demo with vhs
        uses: charmbracelet/vhs-action@v2
        with:
          path: docs/media/demo.tape

      - name: Attach demo.gif to GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: docs/media/demo.gif
