name: CI

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

env:
  CARGO_TERM_COLOR: always
  RUSTFLAGS: -Dwarnings

permissions:
  contents: read

jobs:
  # Check formatting with rustfmt
  formatting:
    name: cargo fmt
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      # Ensure rustfmt is installed and setup problem matcher
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: rustfmt
      - name: Rustfmt Check
        uses: actions-rust-lang/rustfmt@v1

  # Lint with clippy
  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          components: clippy
      - name: Run clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  # Build and test on multiple platforms
  test:
    name: Test (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    needs: [formatting, clippy]
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
      - uses: Swatinem/rust-cache@v2
      - name: Build
        run: cargo build --verbose
      - name: Run doc tests
        run: cargo test --doc --all-features
      - name: Run example tests
        run: cargo test --examples --all-features
      - name: Run tests
        run: cargo test --tests --all-features

  # Run tests using Miri (curated subset)
  miri:
    name: Miri (${{ matrix.os }})
    runs-on: ${{ matrix.os }}
    needs: test
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest]
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: nightly
          components: miri
      - uses: Swatinem/rust-cache@v2
      - name: Install Miri
        run: cargo miri setup
      - name: Test with Miri (core DS + light policies)
        run: |
          cargo miri test --package cachekit --lib ds:: -- --skip concurrent --skip stress --skip large --skip performance --skip memory
          cargo miri test --package cachekit --lib policy:: -- --skip concurrent --skip stress --skip large --skip performance --skip memory
        env:
          MIRIFLAGS: -Zmiri-isolation-error=warn

  # Property-based tests with higher case count
  property-tests:
    name: Property Tests
    runs-on: ubuntu-latest
    needs: test
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
      - uses: Swatinem/rust-cache@v2
      - name: Run property tests with increased cases
        run: PROPTEST_CASES=1000 cargo test --lib property_tests
        env:
          RUST_BACKTRACE: 1

  # Quick fuzz test on PRs (smoke test)
  fuzz-quick:
    name: Fuzz Tests (Quick)
    runs-on: ubuntu-latest
    needs: test
    if: github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: nightly
      - uses: Swatinem/rust-cache@v2
        with:
          workspaces: "fuzz -> target"
      - name: Install cargo-fuzz
        run: cargo install cargo-fuzz
      - name: Run fuzz smoke tests (60s each)
        run: |
          cd fuzz
          # Run arbitrary_ops targets for smoke testing (representative subset)
          # Convention: targets ending in _arbitrary_ops test general operation sequences
          TARGETS=$(cargo fuzz list | grep '_arbitrary_ops$' || cargo fuzz list | head -n 10)

          echo "Running smoke tests for the following targets:"
          echo "$TARGETS"
          echo ""

          seed=1
          for target in $TARGETS; do
            echo "[$seed] Fuzzing $target for 60 seconds..."
            cargo fuzz run "$target" -- -max_total_time=60 -seed=$seed -print_final_stats=1
            seed=$((seed + 1))
            echo ""
          done

  # Documentation build check
  docs:
    name: Documentation
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
      - uses: Swatinem/rust-cache@v2
      - name: Build documentation
        run: cargo doc --no-deps --all-features
        env:
          RUSTDOCFLAGS: -Dwarnings

  # Security audit
  security:
    name: Security Audit
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: rustsec/audit-check@v2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

  # Dependency policy checks across all execution paths
  cargo-deny:
    name: Cargo Deny
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
      - uses: Swatinem/rust-cache@v2
      - name: Install cargo-deny
        run: cargo install --locked cargo-deny
      - name: Run cargo-deny checks
        run: cargo deny check all

  # Benchmarks (only on the main branch)
  bench:
    name: Benchmarks
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: stable
      - uses: Swatinem/rust-cache@v2
      - name: Run benchmarks
        run: cargo bench --no-fail-fast

  # Minimum Supported Rust Version check
  msrv:
    name: MSRV Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: 1.85.0  # MSRV for Rust 2024 edition
      - uses: Swatinem/rust-cache@v2
      - name: Check MSRV
        run: cargo check --all-features
