# .github/workflows/ci.yml
# This is the full GitHub Actions workflow file.

name: CI

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

env:
  CARGO_TERM_COLOR: always
  CARGO_TARGET_DIR: ${{ github.workspace }}/target # Consistent target dir
  RUST_BACKTRACE: 1

jobs:
  # This job will now run your comprehensive script
  comprehensive-tests:
    name: Comprehensive Tests & Benchmarks
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust (stable)
        uses: actions-rs/toolchain@v1.0.7
        with:
          toolchain: stable
          override: true
          profile: minimal # Faster toolchain setup

      - name: Install system prerequisites for tests and benchmarks
        run: |
          sudo apt-get update
          sudo apt-get install -y hyperfine jq bc # For benchmarks within run_all_tests.sh


      - name: Ensure run_all_tests.sh is executable
        run: chmod +x ./run_all_tests.sh

      - name: Check formatting
        run: cargo fmt -- --check

      - name: Lint with Clippy
        run: cargo clippy -- -D warnings

      - name: Run Comprehensive Test Script
        run: ./run_all_tests.sh

      - name: Upload Dirty vs Full Benchmark Report
        uses: actions/upload-artifact@v4
        with:
          name: marlin-dirty-vs-full-benchmark-report
          path: bench/dirty-vs-full.md
          if-no-files-found: warn
          retention-days: 7

      - name: Upload Cold Start Benchmark JSON (if generated by script)
        uses: actions/upload-artifact@v4
        if: ${{ success() }}
        with:
          name: marlin-cold-start-perf-json
          path: perf.json
          if-no-files-found: ignore
          retention-days: 7

      - name: Upload CLI Cheatsheet
        uses: actions/upload-artifact@v4
        with:
          name: marlin-cli-cheatsheet
          path: ${{ github.workspace }}/docs/cli_cheatsheet.md
          if-no-files-found: warn
          retention-days: 7

  coverage:
    name: Code Coverage (Tarpaulin)
    needs: comprehensive-tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Install Rust (nightly for Tarpaulin, if needed)
        uses: actions-rs/toolchain@v1.0.7
        with:
          toolchain: nightly # Or stable if Tarpaulin works well with it for your project
          override: true
          components: llvm-tools-preview # Component needed by Tarpaulin
          profile: minimal

      - name: Install system prerequisites for Tarpaulin
        run: |
          sudo apt-get update
          sudo apt-get install -y pkg-config libssl-dev # Keep if your build needs them

      - name: Install cargo-tarpaulin
        run: cargo install cargo-tarpaulin

      - name: Code Coverage (libmarlin only)
        run: |
          unset MARLIN_DB_PATH
          cargo +nightly tarpaulin --package libmarlin --out Html --out Xml --fail-under 85
        continue-on-error: true

      - name: Upload HTML Coverage Report
        uses: actions/upload-artifact@v4
        with:
          name: marlin-coverage-report-html
          path: tarpaulin-report.html
          if-no-files-found: warn
          retention-days: 7

      - name: Upload XML Coverage Report (for services like Codecov)
        uses: actions/upload-artifact@v4
        with:
          name: marlin-coverage-report-xml
          path: cobertura.xml # Default XML output name for Tarpaulin
          if-no-files-found: warn
          retention-days: 7
