name: CI

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

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  test:
    name: Test Suite
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Run fast tests
        run: make test
      - name: Run comprehensive tests
        run: make test-all

  exhaustive-patterns:
    name: Exhaustive Pattern Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Check compilation
        run: cargo check
      - name: Test exhaustive pattern coverage
        run: cargo test exhaustive_pattern_coverage
      - name: Lint compliance
        run: make lint

  repl-tests:
    name: REPL Test Suite
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: dtolnay/rust-toolchain@nightly
      - uses: Swatinem/rust-cache@v2
      - name: Install test dependencies
        run: |
          cargo install cargo-nextest
          rustup component add llvm-tools-preview
      - name: Run REPL unit tests
        run: cargo test repl --lib
      - name: Run REPL integration tests
        run: |
          cargo test --test repl_commands_test
          cargo test --test cli_oneliner_tests
      - name: Run REPL property tests
        run: cargo test repl_function_tests::property_tests
      - name: Run REPL doctests
        run: cargo test --doc runtime::repl
      - name: Run REPL examples
        run: cargo run --example repl_demo
      - name: Run REPL coverage tests
        run: cargo test --test repl_coverage_tests
      - name: Check REPL coverage
        run: |
          cargo llvm-cov --lib --no-report
          cargo llvm-cov report --lib | grep -E "^\s+runtime::repl" || true

  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2
      - name: Check formatting
        run: cargo fmt --all -- --check
      - name: Run clippy on library
        run: cargo clippy --lib -- -D warnings
      - name: Run clippy on bins
        run: cargo clippy --bins -- -D warnings

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Build debug
        run: cargo build --all-features --workspace
      - name: Build release
        run: cargo build --release --all-features --workspace
      - name: Check examples
        run: cargo build --examples

  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
      - name: Generate coverage
        run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          files: lcov.info
          fail_ci_if_error: false

  check-release:
    name: Check Release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - name: Check package metadata
        run: |
          cargo package --list -p ruchy
          cargo package --list -p ruchy-cli
      - name: Dry run publish
        run: |
          cargo publish --dry-run -p ruchy
          cargo publish --dry-run -p ruchy-cli