name: CI

# CI validates integration and performance on every push to master/main
# and on every pull request. Unit tests moved to the Release workflow
# (release.yml → test-unit job) so that a tag push is gated by passing
# unit tests before any GitHub Release is created.

on:
  push:
    branches: [ master, main ]
  pull_request:
    branches: [ master, main ]

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  test-e2e:
    name: E2E Integration Tests
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      
      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
      
      - name: Cache cargo index
        uses: actions/cache@v4
        with:
          path: ~/.cargo/git
          key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
      
      - name: Cache cargo build
        uses: actions/cache@v4
        with:
          path: target
          key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('**/Cargo.lock') }}
      
      - name: Install dependencies (netcat, time)
        run: sudo apt-get update && sudo apt-get install -y netcat-openbsd time
      
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      
      - name: Run all E2E tests (Fast)
        run: ./tests/run_all_e2e_fast.sh
        timeout-minutes: 60

      - name: Upload E2E logs on failure
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: e2e-logs
          path: |
            tests/.e2e_data/
            tests/.e2e_*/
          retention-days: 7

  test-performance:
    name: Performance Benchmarks
    runs-on: ubuntu-latest
    needs: test-e2e
    if: success()
    steps:
      - uses: actions/checkout@v4

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

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

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

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

      - name: Install dependencies (netcat, time)
        run: sudo apt-get update && sudo apt-get install -y netcat-openbsd time

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Build release
        run: cargo build --release --all-features

      - name: Run Performance Benchmarks
        run: |
          ./tests/benchmark_e2e.sh --focus java_e2e --output-dir /tmp/perf_results
          scripts/compare_perf_metrics.sh /tmp/perf_results .perf_metrics/baseline.json
        timeout-minutes: 10

      - name: Upload performance metrics
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: perf-metrics
          path: /tmp/perf_results/
          retention-days: 30

      - name: Update Baseline (on main/master merge)
        if: github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master'
        run: |
          LATEST_AGG=$(find /tmp/perf_results -name aggregated.json -type f | sort | tail -1)
          if [ -n "$LATEST_AGG" ]; then
            cp "$LATEST_AGG" .perf_metrics/baseline.json
            echo "Baseline updated for $(git rev-parse --short HEAD)"
          else
            echo "WARNING: No aggregated.json found in /tmp/perf_results"
          fi
