name: build-docs
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      environment:
        description: 'The environment to deploy to'
        required: true
        type: choice
        default: 'staging'
        options:
          - staging
          - production

jobs:
  build-docs:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          submodules: "true"

      - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: pip install -e '.[dev]' lazydocs pyyaml

      # setup quarto for rendering example/tutorial nbs
      - uses: quarto-dev/quarto-actions/setup@v2
        with:
          version: 1.4.515

      - name: Build Docs
        run: make all_docs

      - name: Deploy (Push to main or Pull Request)
        if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'pull_request' || (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'staging')
        uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_branch: docs-preview
          publish_dir: docs/mintlify
          user_name: github-actions[bot]
          user_email: 41898282+github-actions[bot]@users.noreply.github.com

      - name: Deploy (Release)
        if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'production')
        uses: peaceiris/actions-gh-pages@4f9cc6602d3f66b9c108549d475ec49e8ef4d45e # v4.0.0
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_branch: docs
          publish_dir: docs/mintlify
          user_name: github-actions[bot]
          user_email: 41898282+github-actions[bot]@users.noreply.github.com


      - name: Trigger mintlify workflow (Push to main or Pull Request)
        if: (github.event_name == 'push' && github.ref == 'refs/heads/main') || github.event_name == 'pull_request' || (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'staging')
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          github-token: ${{ secrets.DOCS_WORKFLOW_TOKEN }}
          script: |
            await github.rest.actions.createWorkflowDispatch({
              owner: 'nixtla',
              repo: 'docs',
              workflow_id: 'mintlify-action-preview.yml',
              ref: 'main',
            });

      - name: Trigger mintlify workflow (Release)
        if: github.event_name == 'release' || (github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'production')
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        with:
          github-token: ${{ secrets.DOCS_WORKFLOW_TOKEN }}
          script: |
            await github.rest.actions.createWorkflowDispatch({
              owner: 'nixtla',
              repo: 'docs',
              workflow_id: 'mintlify-action.yml',
              ref: 'main',
            });
