name: publish

on:
  push:
    tags:
      - "[0-9]+.[0-9]+.[0-9]+"
      - "[0-9]+.[0-9]+.[0-9]+.post[0-9]+"
      - "[0-9]+.[0-9]+.[0-9]+[a-b][0-9]+"
      - "[0-9]+.[0-9]+.[0-9]+rc[0-9]+"
      - "[0-9]+.[0-9]+.[0-9]+.dev[0-9]+"
  workflow_dispatch:

env:
  PYTHON_VERSION: "3.10"

jobs:
  build:
    name: Build the source tarball and the wheel
    runs-on: ubuntu-latest
    environment: release
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: 0

      - name: Install uv (official Astral action)
        uses: astral-sh/setup-uv@v7
        with:
          version: "0.8.9"
          enable-cache: true
          python-version: ${{ env.PYTHON_VERSION }}

      - name: UV build
        run: uv build
      - name: Archive packages
        uses: actions/upload-artifact@v5
        with:
          name: dist
          path: dist

  publish:
    name: Publish build artifacts to the PyPI
    needs: build
    runs-on: ubuntu-latest
    environment: release
    permissions:
      id-token: write
    steps:
      - name: Retrieve packages
        uses: actions/download-artifact@v4
      - name: Upload packages
        uses: pypa/gh-action-pypi-publish@release/v1

  publish-docs:
    name: Publish documentation to GitHub Pages
    runs-on: ubuntu-latest
    needs: build
    permissions:
      pages: write
      id-token: write
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Install dependencies
        run: pnpm install

      - name: Build with VitePress
        run: pnpm run docs:build

      - name: Setup Pages
        uses: actions/configure-pages@v4

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: docs/.vitepress/dist

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

  release:
    name: Create GitHub release
    runs-on: ubuntu-latest
    needs: build
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: 0

      - name: Install uv (official Astral action)
        uses: astral-sh/setup-uv@v7
        with:
          version: "0.8.9"
          enable-cache: true
          python-version: ${{ env.PYTHON_VERSION }}

      - name: Set up Python (using uv)
        run: uv python install

      - name: Download packages
        uses: actions/download-artifact@v4
        with:
          name: dist

      - name: Generate a changelog
        uses: orhun/git-cliff-action@v4
        with:
          args: "0.1.3.. --latest"
        env:
          OUTPUT: release-notes.md
          GITHUB_REPO: ${{ github.repository }}

      - name: Create release with assets
        uses: softprops/action-gh-release@v2
        with:
          files: ./dist/*
          body_path: release-notes.md
