name: Code Quality

# This workflow is now orchestrated by main.yml
# To run manually, use workflow_dispatch
on:
  workflow_dispatch:

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1
  RUST_MIN_STACK: 8388608

defaults:
  run:
    shell: bash

jobs:
  coverage-check:
    name: Coverage Check
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
      
      - name: Install shellcheck
        run: |
          sudo apt-get update
          sudo apt-get install -y shellcheck
      
      - name: Generate coverage report
        run: |
          make server-test
          make coverage-summary > coverage-summary.txt
      
      - name: Check coverage threshold
        run: |
          coverage=$(make coverage-summary | grep "TOTAL" | awk '{print $10}' | sed 's/%//')
          echo "Current coverage: $coverage%"
          # Use awk for floating point comparison instead of bc
          if awk "BEGIN {exit !($coverage < 60)}"; then
            echo "Coverage is below 60% threshold"
            exit 1
          fi
      
      - name: Upload coverage report
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage/
      
      - name: Comment PR with coverage
        uses: actions/github-script@v7
        if: github.event_name == 'pull_request'
        with:
          script: |
            const fs = require('fs');
            const coverageSummary = fs.readFileSync('coverage-summary.txt', 'utf8');
            const match = coverageSummary.match(/Total.*?(\d+\.\d+)%/);
            const percentage = match ? match[1] : 'Unknown';
            
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `## Coverage Report 📊\n\n**Total Coverage:** ${percentage}%\n\n✅ Coverage threshold (60%) ${parseFloat(percentage) >= 60 ? 'passed' : 'failed'}\n\n[View full report](https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})`
            });

  complexity-check:
    name: Code Complexity Check
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      
      - name: Cache complexity tools
        uses: actions/cache@v4
        with:
          path: ~/.cargo/bin
          key: ${{ runner.os }}-cargo-tools-${{ hashFiles('**/Cargo.lock') }}
      
      - name: Install complexity tools
        run: |
          make install-dev-tools
      
      - name: Count lines of code
        run: |
          echo "## Lines of Code Report" >> $GITHUB_STEP_SUMMARY
          echo '```' >> $GITHUB_STEP_SUMMARY
          make server-tokei >> $GITHUB_STEP_SUMMARY
          echo '```' >> $GITHUB_STEP_SUMMARY
      
      - name: Check for unsafe code
        run: |
          echo "## Unsafe Code Report" >> $GITHUB_STEP_SUMMARY
          echo '```' >> $GITHUB_STEP_SUMMARY
          make cargo-geiger >> $GITHUB_STEP_SUMMARY || true
          echo '```' >> $GITHUB_STEP_SUMMARY

  documentation-check:
    name: Documentation Check
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      
      - name: Check documentation
        run: |
          make cargo-doc
      
      - name: Check for missing docs
        run: |
          make cargo-rustdoc
      
      - name: Upload documentation
        uses: actions/upload-artifact@v4
        with:
          name: documentation
          path: target/doc/