name: CI

# ⚠️ IMPORTANT: This project uses a Cargo workspace structure.
# All commands MUST be run from the repository root using the root Makefile.
# DO NOT use 'cd server && make ...' patterns - use 'make server-...' instead.
# See .github/CONTRIBUTING.md for details.

# 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:
  build-and-test:
    name: Build and Test
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      
      - name: Install Deno
        uses: denoland/setup-deno@v2
        with:
          deno-version: v2.x
      
      - name: Install cargo-llvm-cov
        uses: taiki-e/install-action@cargo-llvm-cov
      
      - name: Install cargo-nextest
        uses: taiki-e/install-action@cargo-nextest
      
      - name: Install shellcheck
        run: |
          sudo apt-get update
          sudo apt-get install -y shellcheck
      
      - name: Setup Rust cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: ". -> target"
          cache-on-failure: true
          shared-key: "ci-build"
      
      - name: Build dependencies first
        run: |
          echo "📦 Pre-building dependencies to maximize cache usage..."
          cargo build --package pmat --lib
        timeout-minutes: 10
      
      - name: Run format check
        run: make format
        timeout-minutes: 2
      
      - name: Run lint check
        run: |
          echo "🔍 Running lint checks..."
          cargo clippy --package pmat -- -W warnings
        timeout-minutes: 5
      
      - name: Run type check
        run: |
          echo "✅ Running type check..."
          cargo check --package pmat --all-targets
        timeout-minutes: 3
      
      - name: Build binary
        run: |
          echo "🔨 Building binary..."
          cargo build --package pmat --bin pmat
        timeout-minutes: 5
      
      - name: Run unit tests
        run: |
          echo "🧪 Running minimal smoke tests..."
          cd server
          # Run only a few fast tests as smoke test
          cargo test --lib cli::analysis::tests::test_mod_basic -- --nocapture
        timeout-minutes: 3
        env:
          RUST_BACKTRACE: 1
          SKIP_SLOW_TESTS: true
          CI: true

      - name: Run integration tests (CLI handlers)
        run: |
          echo "🔌 Running integration tests for CLI handlers..."
          echo "   These test handlers/dispatchers excluded from coverage measurement"
          make test-integration
        timeout-minutes: 5
        env:
          RUST_BACKTRACE: 1
          CI: true
      
      - name: Upload binary artifact
        uses: actions/upload-artifact@v4
        with:
          name: pmat-linux
          path: target/debug/pmat
      
      - name: Upload coverage report
        uses: actions/upload-artifact@v4
        with:
          name: coverage-report
          path: coverage/
        if: always()

  documentation-sync:
    name: Documentation Sync Tests
    runs-on: ubuntu-22.04
    needs: build-and-test
    steps:
      - uses: actions/checkout@v4

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

      - name: Install cargo-nextest
        uses: taiki-e/install-action@cargo-nextest

      - name: Download binary artifact
        uses: actions/download-artifact@v4
        with:
          name: pmat-linux
          path: target/debug/

      - name: Make binary executable
        run: chmod +x target/debug/pmat

      - name: Validate documentation links
        run: |
          echo "🔗 Validating documentation links..."
          ./target/debug/pmat validate-docs --root docs --fail-on-error
        timeout-minutes: 3

      - name: Run documentation sync tests
        run: |
          echo "📚 Running documentation synchronization tests..."
          cd server
          cargo nextest run --test cli_documentation_sync || cargo test --test cli_documentation_sync
          # Note: MCP tests are skipped in CI (they spawn MCP server processes which can hang)
          echo "⏭️ Skipping MCP documentation sync tests in CI (they run locally)"
          cargo nextest run --test documentation_examples || cargo test --test documentation_examples
      
      - name: Verify all documentation tests pass
        run: |
          cd server
          echo "✅ Documentation sync tests completed successfully"
          echo "Note: MCP server tests are skipped in CI and run locally for validation"

  dogfooding:
    name: Dogfooding - Test Our Own Tools
    runs-on: ubuntu-22.04
    needs: build-and-test
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Deno for dogfooding script
        uses: denoland/setup-deno@v2
        with:
          deno-version: v2.x
      
      - name: Download binary artifact
        uses: actions/download-artifact@v4
        with:
          name: pmat-linux
          path: target/debug/
      
      - name: Make binary executable
        run: chmod +x target/debug/pmat
      
      - name: Dogfood - Use our own tools for CI metrics
        run: |
          echo "🐕 Running dogfooding: Using our own MCP toolkit to analyze our codebase"
          make dogfood-ci
      
      - name: Upload dogfooding metrics
        uses: actions/upload-artifact@v4
        with:
          name: dogfooding-metrics
          path: artifacts/dogfooding/
        if: always()
      
      - name: Display metrics summary
        run: |
          echo "📊 Dogfooding Results:"
          echo "====================="
          if [ -f artifacts/dogfooding/complexity-latest.txt ]; then
            echo "🔍 Complexity Analysis:"
            head -20 artifacts/dogfooding/complexity-latest.txt
          fi
          echo ""
          if [ -f artifacts/dogfooding/churn-latest.txt ]; then
            echo "🔄 Code Churn (Last 7 Days):"
            head -10 artifacts/dogfooding/churn-latest.txt
          fi
          echo ""
          if [ -f artifacts/dogfooding/dag-latest.mmd ]; then
            echo "🔀 Dependency Graph Generated:"
            wc -l artifacts/dogfooding/dag-latest.mmd
          fi
        if: always()

  security:
    name: Security Audit
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
      
      - name: Install cargo-audit
        uses: taiki-e/install-action@cargo-audit
      
      - name: Run security audit via Makefile
        run: |
          make audit