name: Build CLI

# Build CLI workflow - only runs on workflow_call (from release.yml) or manual trigger.
# PR checks build CLI via pr-checks.yml.
# Main branch pushes are handled by release.yml to avoid duplicate CI runs.

on:
  workflow_dispatch:
  workflow_call:

permissions:
  contents: read
  actions: write

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  build:
    name: Build CLI (${{ matrix.target }})
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        include:
          # Windows
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact: auroraview-cli.exe
            archive: auroraview-cli-windows-x64.zip
          # macOS Universal (build both archs on ARM runner)
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: auroraview-cli
            archive: auroraview-cli-macos-arm64.tar.gz
          # macOS Intel (cross-compile on ARM runner)
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact: auroraview-cli
            archive: auroraview-cli-macos-x64.tar.gz
          # Linux
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            artifact: auroraview-cli
            archive: auroraview-cli-linux-x64.tar.gz

    steps:
      - uses: actions/checkout@v6

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

      - name: Install Linux dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev libxdo-dev

      - name: Build CLI
        run: cargo build -p auroraview-cli --release --target ${{ matrix.target }}

      - name: Package (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          $artifact = "target/${{ matrix.target }}/release/${{ matrix.artifact }}"
          Compress-Archive -Path $artifact -DestinationPath ${{ matrix.archive }}

      - name: Package (Unix)
        if: runner.os != 'Windows'
        run: |
          cd target/${{ matrix.target }}/release
          tar -czvf ../../../${{ matrix.archive }} ${{ matrix.artifact }}

      - name: Upload artifact
        uses: actions/upload-artifact@v6
        with:
          name: cli-${{ matrix.target }}
          path: ${{ matrix.archive }}
          if-no-files-found: error

  # Combine all artifacts into one
  combine-artifacts:
    name: Combine CLI Artifacts
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v7
        with:
          pattern: cli-*
          path: artifacts
          merge-multiple: true

      - name: List artifacts
        run: ls -la artifacts/

      - name: Upload combined artifact
        uses: actions/upload-artifact@v6
        with:
          name: cli-all-platforms
          path: artifacts/*
          if-no-files-found: error

