name: CI

permissions:
  contents: read

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

env:
  CARGO_TERM_COLOR: always

jobs:
  test:
    name: Test Suite
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest]
        rust: [stable]
        include:
          # Test MSRV (Minimum Supported Rust Version)
          - os: ubuntu-latest
            rust: 1.92.0
          # Test nightly for additional checks
          - os: ubuntu-latest
            rust: nightly

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

      - name: Install Rust
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: ${{ matrix.rust }}

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - name: Check formatting
        run: cargo fmt --all -- --check
        if: matrix.os == 'ubuntu-latest' && matrix.rust == 'stable'

      - name: Run clippy
        run: cargo clippy -- -D warnings
        if: matrix.os == 'ubuntu-latest' && matrix.rust == 'stable'

      - name: Build
        run: cargo build --verbose

      - name: Run tests
        run: cargo test --verbose

      - name: Run integration tests
        run: cargo test --test integration_test --verbose
        env:
          RUST_LOG: debug

      - name: Generate documentation
        run: cargo doc --no-deps
        if: matrix.os == 'ubuntu-latest' && matrix.rust == 'stable'

      - name: Check documentation examples
        run: cargo test --doc
        if: matrix.os == 'ubuntu-latest' && matrix.rust == 'stable'

  security-audit:
    name: Security Audit
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Install cargo-audit
        run: cargo install cargo-audit

      - name: Run security audit
        run: cargo audit

  coverage:
    name: Code Coverage
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

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

      - name: Generate coverage report
        run: cargo tarpaulin --out Xml --output-dir coverage --exclude-files 'tests/*' --exclude-files 'examples/*'

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v3
        with:
          file: ./coverage/cobertura.xml
          fail_ci_if_error: false

  quality-gates:
    name: Quality Gates
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@master
        with:
          toolchain: stable
          components: rustfmt, clippy

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

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

      - name: Run enhanced clippy checks
        run: |
          cargo clippy --all-targets --all-features -- \
            -D warnings \
            -D clippy::all \
            -D clippy::pedantic \
            -D clippy::nursery \
            -D clippy::cargo \
            -A clippy::multiple_crate_versions \
            -A clippy::missing_errors_doc \
            -A clippy::missing_panics_doc

      - name: Check for unused dependencies
        run: |
          cargo install cargo-machete
          cargo machete

      - name: Check documentation coverage
        run: |
          RUSTDOCFLAGS="-D missing_docs -D rustdoc::broken_intra_doc_links" \
          cargo doc --all-features --no-deps

      - name: Verify no TODO/FIXME comments in production code
        run: |
          if grep -r "TODO\|FIXME" src/; then
            echo "Found TODO/FIXME comments in production code"
            exit 1
          fi

  performance-tests:
    name: Performance Tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

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

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - name: Run performance tests
        run: |
          # Build optimized version for performance testing
          cargo build --release --all-features

          # Check binary size (fail if too large)
          size=$(stat -c%s target/release/librainy_sdk.rlib 2>/dev/null || echo "0")
          max_size=$((10 * 1024 * 1024))  # 10MB limit

          if [ "$size" -gt "$max_size" ]; then
            echo "Binary size ($size bytes) exceeds limit ($max_size bytes)"
            exit 1
          fi

          echo "Binary size check passed: $size bytes"

      - name: Compilation time check
        run: |
          # Measure compilation time
          start_time=$(date +%s)
          cargo clean
          cargo build --release
          end_time=$(date +%s)
          compile_time=$((end_time - start_time))

          echo "Compilation time: ${compile_time} seconds"

          # Fail if compilation takes too long (5 minutes)
          if [ "$compile_time" -gt 300 ]; then
            echo "Compilation time exceeds 5 minutes"
            exit 1
          fi
