name: CI

on:
  push:
    branches: [main]
  pull_request:

jobs:
  test:
    name: Test Outfit
    runs-on: ubuntu-latest

    strategy:
      matrix:
        include:
          - name: Default features
            features: ""
          - name: With jpl-download feature
            features: "--features jpl-download"

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

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

      - name: Run tests (${{ matrix.name }})
        run: |
          echo "Running cargo test with features: '${{ matrix.features }}'"
          cargo test --locked --all-targets ${{ matrix.features }}

  fmt:
    name: Check formatting
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Install Rust with rustfmt
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: Run cargo fmt
        run: cargo fmt --all -- --check

  clippy:
    name: Clippy lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Install Rust with clippy
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - name: Cache cargo registry
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
      - name: check version
        run: |
          rustc --version
          cargo clippy --version
      - name: Run cargo clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  coverage:
    name: Coverage (stable, cargo-llvm-cov → Codecov)
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust stable + llvm-tools
        uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview

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

      - name: Install cargo-llvm-cov
        run: cargo install cargo-llvm-cov

      - name: Clean coverage artifacts
        run: cargo llvm-cov clean --workspace

      - name: Generate coverage (lcov)
        run: |
          cargo llvm-cov \
            --workspace \
            --all-features \
            --lcov --output-path lcov.info

      - name: Upload to Codecov
        uses: codecov/codecov-action@v4
        with:
          files: lcov.info
          flags: rust
          fail_ci_if_error: true
          token: ${{ secrets.CODECOV_TOKEN }}
  # Not yet on crate.io so disabled for now
  # semver-pr:
  #   name: SemVer (PR vs base)
  #   runs-on: ubuntu-latest
  #   steps:
  #     - name: Checkout repository
  #       uses: actions/checkout@v4

  #     - name: Check semver (baseline = PR base)
  #       uses: obi1kenobi/cargo-semver-checks-action@v2
  #       with:
  #         feature-group: all-features
  #         baseline-rev: ${{ github.event.pull_request.base.sha }}