name: CI

on:
  push:
    branches: [main]
    tags: ['v*']
  pull_request:
    branches: [main]

permissions:
  pull-requests: write
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo test
      - run: cargo clippy -- -D warnings

  test-enrich:
    name: Test (with enrich)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo test --features enrich

  # After tests pass on main: create release PR + tag/publish on merge
  release-plz:
    name: Release-plz
    needs: [test, test-enrich]
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
          token: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }}

      - uses: dtolnay/rust-toolchain@stable

      - name: Release PR
        uses: release-plz/action@v0.5
        with:
          command: release-pr
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }}

      - name: Release
        uses: release-plz/action@v0.5
        with:
          command: release
        env:
          GITHUB_TOKEN: ${{ secrets.RELEASE_TOKEN || secrets.GITHUB_TOKEN }}
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

  # Build binaries on version tags
  build:
    name: Build (${{ matrix.platform }})
    needs: [test, test-enrich]
    if: startsWith(github.ref, 'refs/tags/v')
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
            platform: linux-x86_64
          - os: ubuntu-latest
            target: aarch64-unknown-linux-gnu
            platform: linux-aarch64
            cross: true
          - os: macos-latest
            target: x86_64-apple-darwin
            platform: darwin-x86_64
          - os: macos-latest
            target: aarch64-apple-darwin
            platform: darwin-aarch64
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            platform: windows-x86_64
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v6

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

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

      - name: Build lean binary
        shell: bash
        run: |
          BUILD_CMD="cargo"
          if [ "${{ matrix.cross }}" = "true" ]; then BUILD_CMD="cross"; fi
          $BUILD_CMD build --release --target ${{ matrix.target }}

      - name: Stage lean binary
        shell: bash
        run: |
          mkdir -p staged
          if [ "${{ runner.os }}" = "Windows" ]; then
            cp target/${{ matrix.target }}/release/arai.exe staged/arai-${{ matrix.platform }}.exe
          else
            cp target/${{ matrix.target }}/release/arai staged/arai-${{ matrix.platform }}
            chmod +x staged/arai-${{ matrix.platform }}
          fi

      - name: Build full binary
        continue-on-error: true
        shell: bash
        run: |
          BUILD_CMD="cargo"
          if [ "${{ matrix.cross }}" = "true" ]; then BUILD_CMD="cross"; fi
          $BUILD_CMD build --release --target ${{ matrix.target }} --features enrich

      - name: Stage full binary
        continue-on-error: true
        shell: bash
        run: |
          if [ "${{ runner.os }}" = "Windows" ]; then
            if [ -f target/${{ matrix.target }}/release/arai.exe ]; then
              cp target/${{ matrix.target }}/release/arai.exe staged/arai-full-${{ matrix.platform }}.exe
            fi
          else
            if [ -f target/${{ matrix.target }}/release/arai ]; then
              cp target/${{ matrix.target }}/release/arai staged/arai-full-${{ matrix.platform }}
              chmod +x staged/arai-full-${{ matrix.platform }}
            fi
          fi

      - name: Upload artifacts
        uses: actions/upload-artifact@v7
        with:
          name: binaries-${{ matrix.platform }}
          path: staged/

  attach-binaries:
    name: Attach binaries to release
    needs: build
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

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

      - name: Generate checksums
        run: |
          cd binaries
          sha256sum * > checksums.txt
          cat checksums.txt

      - name: Attach to release
        uses: softprops/action-gh-release@v3
        with:
          files: binaries/*
