# Documentation deployment workflow
# - Deploys to GitHub Pages on push to main
# - Builds preview for PRs
#
# IMPORTANT: Before first deployment, you must enable GitHub Pages in repository settings:
# 1. Go to Settings → Pages
# 2. Under "Build and deployment", select "GitHub Actions" as the source
# This is required because GITHUB_TOKEN doesn't have permission to create Pages sites.

name: Documentation

on:
  push:
    branches:
      - main
    paths:
      - 'docs/**'
      - '.github/workflows/docs.yml'
  pull_request:
    branches:
      - main
    paths:
      - 'docs/**'
      - '.github/workflows/docs.yml'
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment
concurrency:
  group: pages-${{ github.ref }}
  cancel-in-progress: true

jobs:
  # Build job
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install dependencies
        working-directory: docs
        run: npm install

      - name: Copy logo
        run: |
          mkdir -p docs/public
          cp assets/icons/auroraview-logo-text.png docs/public/logo.png || echo "Logo not found, using placeholder"

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

      - name: Upload artifact for deployment
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        uses: actions/upload-pages-artifact@v4
        with:
          path: docs/.vitepress/dist

      - name: Upload artifact for PR preview
        if: github.event_name == 'pull_request'
        uses: actions/upload-artifact@v6
        with:
          name: docs-preview-${{ github.event.pull_request.number }}
          path: docs/.vitepress/dist
          retention-days: 7

  # Deploy to GitHub Pages (only on main)
  deploy:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Setup Pages
        uses: actions/configure-pages@v5

      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

  # PR Preview comment
  preview-comment:
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    needs: build
    permissions:
      pull-requests: write
    steps:
      - name: Find Comment
        uses: peter-evans/find-comment@v4
        id: fc
        with:
          issue-number: ${{ github.event.pull_request.number }}
          comment-author: 'github-actions[bot]'
          body-includes: '📚 Documentation Preview'

      - name: Create or update comment
        uses: peter-evans/create-or-update-comment@v4
        with:
          comment-id: ${{ steps.fc.outputs.comment-id }}
          issue-number: ${{ github.event.pull_request.number }}
          body: |
            ## 📚 Documentation Preview

            The documentation has been built successfully!

            **Build Status:** ✅ Success

            To preview the documentation locally:
            1. Download the artifact from the [Actions run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
            2. Extract and serve with any static file server

            Or wait for the PR to be merged to see the changes on the live site.

            ---
            *This comment is automatically updated on each push to this PR.*
          edit-mode: replace
