name: Deploy Documentation

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

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

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          fetch-depth: 0

      - name: Set up uv
        uses: astral-sh/setup-uv@08807647e7069bb48b6ef5acd8ec9567f424441b
        with:
          python-version: '3.12'
          enable-cache: true

      - name: Install dependencies
        run: uv sync --extra docs

      - name: Build docs (PR preview)
        if: github.event_name == 'pull_request'
        run: uv run mkdocs build --strict

      - name: Configure git for deployment
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: Deploy with mike
        if: github.event_name == 'push' && github.ref == 'refs/heads/main'
        run: |
          uv run mike deploy --push --update-aliases latest stable
          uv run mike set-default --push stable
