name: Build and Deploy Documentation

# Add permissions to allow GitHub Actions to push to gh-pages
permissions:
  contents: write

on:
  push:
    branches:
      - main
    paths:
      - 'uim-docs/**'
      - '.github/workflows/docs.yml'
      - 'pyproject.toml'
      - 'poetry.lock'
  pull_request:
    branches:
      - main
    paths:
      - 'uim-docs/**'
      - '.github/workflows/docs.yml'
      - 'pyproject.toml'
      - 'poetry.lock'
  workflow_dispatch:

jobs:
  build:
    name: Build and Deploy Documentation
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'

      - name: Install Poetry
        uses: snok/install-poetry@v1
        with:
          version: 1.7.1
          virtualenvs-create: true
          virtualenvs-in-project: true

      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v3
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}

      - name: Install dependencies
        run: |
          poetry install --only docs

      - name: Build documentation
        run: |
          cd uim-docs
          poetry run mkdocs build --verbose --site-dir ../site

      - name: Upload documentation artifact
        uses: actions/upload-artifact@v4
        with:
          name: documentation-site
          path: site/
          retention-days: 1

      - name: Deploy to GitHub Pages
        if: github.event_name != 'pull_request'
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./site
          force_orphan: true
          user_name: 'github-actions[bot]'
          user_email: 'github-actions[bot]@users.noreply.github.com'
          commit_message: 'Deploy documentation to GitHub Pages'
