name: Documentation

on:
  # Build validation on PRs
  pull_request:
    paths:
      - 'docs/**'
      - 'website/**'
      - '.github/workflows/docs.yml'

  # Deploy on push to main
  push:
    branches:
      - main
    paths:
      - 'docs/**'
      - 'website/**'
      - '.github/workflows/docs.yml'

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5  # v4.3.1

      - name: Setup Node.js
        uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020  # v4.4.0
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: website/package-lock.json

      - name: Install dependencies
        working-directory: website
        run: npm ci

      - name: Build documentation
        working-directory: website
        run: npm run build

      - name: Verify build output
        run: |
          if [ ! -f website/build/index.html ]; then
            echo "::error::Documentation build failed - no index.html found"
            exit 1
          fi

          if [ ! -f website/build/llms.txt ]; then
            echo "::warning::llms.txt not generated"
          fi

          echo "Documentation build successful!"
          echo "Build size: $(du -sh website/build | cut -f1)"
          echo "Pages: $(find website/build -name '*.html' | wc -l)"

      # Deploy to Cloudflare Pages on push to main (skipped for forks/PRs)
      - name: Deploy to Cloudflare Pages
        if: |
          github.event_name == 'push' &&
          github.ref == 'refs/heads/main' &&
          github.repository == 'smart-mcp-proxy/mcpproxy-go'
        uses: cloudflare/wrangler-action@9681c2997648301493e78cacbfb790a9f19c833f  # v3.9.0
        with:
          apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
          command: pages deploy website/build --project-name=mcpproxy-docs
        continue-on-error: true
