# Claude Context Engineering - Documentation Validation
#
# This workflow validates that documentation is accurate and up-to-date.
# Copy this file to .github/workflows/validate-docs.yml to enable.
#
# Runs on:
# - Pull requests that modify .ai-context/ or source files
# - Manual trigger

name: Validate Documentation

on:
  pull_request:
    paths:
      - '.ai-context/**'
      - 'src/**'
      - 'lib/**'
      - 'app/**'
  workflow_dispatch:

jobs:
  validate:
    name: Validate Claude Context Documentation
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '18'
          cache: 'npm'
          cache-dependency-path: '.ai-context/tools/package-lock.json'

      - name: Install CLI tools
        run: |
          cd .ai-context/tools
          npm ci

      - name: Validate JSON Schemas
        run: |
          echo "🔍 Validating JSON schemas..."
          npx .ai-context/tools/bin/claude-context.js validate --schema
        continue-on-error: false

      - name: Validate Markdown Links
        run: |
          echo "🔗 Checking markdown links..."
          npx .ai-context/tools/bin/claude-context.js validate --links
        continue-on-error: false

      - name: Check for Unresolved Placeholders
        run: |
          echo "📝 Checking for placeholders..."
          npx .ai-context/tools/bin/claude-context.js validate --placeholders
        continue-on-error: true  # Warning only

      - name: Validate Directory Structure
        run: |
          echo "📁 Validating structure..."
          npx .ai-context/tools/bin/claude-context.js validate --structure
        continue-on-error: false

      - name: Check Line Number Accuracy
        run: |
          echo "📍 Checking line number accuracy..."
          npx .ai-context/tools/bin/claude-context.js validate --lines --threshold 60
        continue-on-error: true  # Warning only for line drift

      - name: Generate Validation Report
        if: always()
        run: |
          echo "📊 Generating full validation report..."
          npx .ai-context/tools/bin/claude-context.js validate --all --output markdown > validation-report.md
          cat validation-report.md

      - name: Upload Validation Report
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: validation-report
          path: validation-report.md
          retention-days: 7

      - name: Comment on PR
        if: github.event_name == 'pull_request' && failure()
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            let report = '';
            try {
              report = fs.readFileSync('validation-report.md', 'utf8');
            } catch (e) {
              report = 'Validation report not available.';
            }

            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `## 🔍 Documentation Validation Failed\n\n${report}\n\n---\n*Generated by Claude Context Engineering CI*`
            });
