name: Test Packages

on:
  push:
    branches: ["main"]
    paths:
      - 'packaging/**'
      - 'scripts/package.sh'
      - 'testing/**'
      - '.github/workflows/test-packages.yml'
  pull_request:
    branches: ["main"]
    paths:
      - 'packaging/**'
      - 'scripts/package.sh'
      - 'testing/**'
  workflow_dispatch:
    inputs:
      distros:
        description: 'Distros to test (space-separated, or "all")'
        required: false
        default: 'all'

env:
  CARGO_TERM_COLOR: always

jobs:
  build-packages:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-action@stable

      - name: Install build dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libasound2-dev \
            libclang-dev \
            cmake \
            ruby \
            ruby-dev \
            build-essential
          sudo gem install fpm

      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Build tiered binaries
        run: |
          # Build AVX2 baseline (disable AVX-512 via compiler flags)
          # Note: WHISPER_NO_AVX512=ON doesn't work with whisper-rs-sys 0.14.1
          echo "Building AVX2 baseline..."
          CMAKE_C_FLAGS="-mno-avx512f" CMAKE_CXX_FLAGS="-mno-avx512f" cargo build --release
          mkdir -p releases/test
          cp target/release/voxtype releases/test/voxtype-test-linux-x86_64-avx2

          # Build AVX-512 (on CI this produces same as AVX2 since runners lack AVX-512)
          # Clean build cache to ensure whisper.cpp recompiles with different flags
          echo "Building AVX-512 optimized..."
          cargo clean
          cargo build --release
          cp target/release/voxtype releases/test/voxtype-test-linux-x86_64-avx512

      - name: Build packages with fpm
        run: |
          VERSION="test"
          RELEASE_DIR="releases/test"
          STAGING="/tmp/voxtype-package"

          rm -rf "$STAGING"
          mkdir -p "$STAGING"/{usr/bin,usr/lib/voxtype,etc/voxtype,usr/lib/systemd/user,usr/share/doc/voxtype}
          mkdir -p "$STAGING"/usr/share/{bash-completion/completions,zsh/site-functions,fish/vendor_completions.d}

          # Copy tiered binaries
          cp "$RELEASE_DIR/voxtype-test-linux-x86_64-avx2" "$STAGING/usr/lib/voxtype/voxtype-avx2"
          cp "$RELEASE_DIR/voxtype-test-linux-x86_64-avx512" "$STAGING/usr/lib/voxtype/voxtype-avx512"
          chmod 755 "$STAGING/usr/lib/voxtype/"*

          # Copy other files
          cp config/default.toml "$STAGING/etc/voxtype/config.toml"
          cp packaging/systemd/voxtype.service "$STAGING/usr/lib/systemd/user/"
          cp README.md LICENSE "$STAGING/usr/share/doc/voxtype/"
          cp packaging/completions/voxtype.bash "$STAGING/usr/share/bash-completion/completions/voxtype"
          cp packaging/completions/voxtype.zsh "$STAGING/usr/share/zsh/site-functions/_voxtype"
          cp packaging/completions/voxtype.fish "$STAGING/usr/share/fish/vendor_completions.d/voxtype.fish"

          # Create post-install script
          cat > "$STAGING/postinstall.sh" << 'EOF'
          #!/bin/bash
          rm -f /usr/bin/voxtype
          if grep -q avx512f /proc/cpuinfo 2>/dev/null; then
              ln -sf /usr/lib/voxtype/voxtype-avx512 /usr/bin/voxtype
          else
              ln -sf /usr/lib/voxtype/voxtype-avx2 /usr/bin/voxtype
          fi
          EOF
          chmod +x "$STAGING/postinstall.sh"

          # Create post-uninstall script
          cat > "$STAGING/postuninstall.sh" << 'EOF'
          #!/bin/bash
          rm -f /usr/bin/voxtype
          EOF
          chmod +x "$STAGING/postuninstall.sh"

          # Build deb
          fpm -s dir -t deb \
            --name voxtype \
            --version "$VERSION" \
            --architecture x86_64 \
            --maintainer "test" \
            --description "Test build" \
            --after-install "$STAGING/postinstall.sh" \
            --after-remove "$STAGING/postuninstall.sh" \
            --depends "libasound2 | libasound2t64" \
            --depends libc6 \
            --package "$RELEASE_DIR/voxtype_${VERSION}_amd64.deb" \
            -C "$STAGING" \
            usr etc

          # Build rpm
          fpm -s dir -t rpm \
            --name voxtype \
            --version "$VERSION" \
            --architecture x86_64 \
            --maintainer "test" \
            --description "Test build" \
            --after-install "$STAGING/postinstall.sh" \
            --after-remove "$STAGING/postuninstall.sh" \
            --depends "alsa-lib" \
            --depends "glibc" \
            --package "$RELEASE_DIR/voxtype-${VERSION}.x86_64.rpm" \
            -C "$STAGING" \
            usr etc

          ls -la "$RELEASE_DIR"

      - name: Upload packages
        uses: actions/upload-artifact@v4
        with:
          name: packages
          path: releases/test/

  test-debian:
    needs: build-packages
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        image: ['debian:bookworm-slim', 'debian:sid-slim', 'ubuntu:24.04']
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download packages
        uses: actions/download-artifact@v4
        with:
          name: packages
          path: releases/test/

      - name: Test on ${{ matrix.image }}
        run: |
          docker run --rm \
            -v ${{ github.workspace }}/releases/test:/packages:ro \
            -v ${{ github.workspace }}/testing/test-install.sh:/test-install.sh:ro \
            ${{ matrix.image }} \
            bash -c "
              apt-get update && apt-get install -y libasound2 libasound2t64 2>/dev/null || apt-get install -y libasound2
              chmod +x /test-install.sh
              /test-install.sh debian
            "

  test-fedora:
    needs: build-packages
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download packages
        uses: actions/download-artifact@v4
        with:
          name: packages
          path: releases/test/

      - name: Test on Fedora
        run: |
          docker run --rm \
            -v ${{ github.workspace }}/releases/test:/packages:ro \
            -v ${{ github.workspace }}/testing/test-install.sh:/test-install.sh:ro \
            fedora:41 \
            bash -c "
              dnf install -y alsa-lib
              chmod +x /test-install.sh
              /test-install.sh rpm
            "
