name: Deploy API Docs

on:
  workflow_dispatch:
  push:
    branches: [main]
    paths:
      - 'api/openapi.yaml'
      - '.forgejo/workflows/docs-deploy.yml'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read
  pages: write
  id-token: write

jobs:
  build-docs:
    name: Build API Documentation
    runs-on: self-hosted
    timeout-minutes: 10

    steps:
    - name: Checkout code
      uses: actions/checkout@v5

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

    - name: Install Redocly CLI
      run: npm install -g @redocly/cli

    - name: Generate documentation
      run: |
        mkdir -p docs/api
        redocly build-docs api/openapi.yaml \
          --output docs/api/index.html \
          --title "FetchML API"

    - name: Upload artifact
      uses: actions/upload-pages-artifact@v3
      with:
        path: docs/api

  deploy-docs:
    name: Deploy to GitHub Pages
    needs: build-docs
    runs-on: self-hosted
    timeout-minutes: 10

    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}

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