name: Deploy Documentation

on:
  push:
    branches: [master]
    paths:
      - "docs/**"
      - "src/**"
      - "crates/**"
      - ".github/workflows/deploy-docs.yml"
  workflow_dispatch:

jobs:
  deploy-docs:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          components: rust-docs

      - name: Setup mdBook
        uses: peaceiris/actions-mdbook@v2
        with:
          mdbook-version: "0.4.40"

      - name: Build API documentation
        run: |
          set -euo pipefail
          cargo doc --no-deps
          echo "✅ API documentation built successfully"

      - name: Build mdBook
        run: |
          set -euo pipefail
          cd docs
          mdbook build
          echo "✅ mdBook built successfully"

      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: |
            target/doc
            docs/book
          publish_branch: gh-pages
          keep_files: false
          user_name: "github-actions[bot]"
          user_email: "github-actions[bot]@users.noreply.github.com"
          commit_message: "Deploy documentation to GitHub Pages"

      - name: Deployment Summary
        run: |
          set -euo pipefail
          echo "📚 Documentation deployed successfully!"
          echo ""
          echo "📍 API Docs: https://seanchatmangpt.github.io/ggen/target/doc/"
          echo "📍 Guides: https://seanchatmangpt.github.io/ggen/docs/book/"
          echo ""
          echo "✅ Deployment complete!"
