name: Upload Python Package

on:
  release:
    types: [published]

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

concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.x'
    - uses: astral-sh/setup-uv@v4
      with:
        version: "latest"
    - name: Install dependencies
      run: |
        uv sync
    - name: Build package
      run: uv build
    - name: Publish package
      run: |
        uv publish --token "$password"
      env:
        password: ${{ secrets.PYPI_API_TOKEN }}

  build-docs:
    runs-on: ubuntu-latest
    needs: deploy
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Pages
        uses: actions/configure-pages@v4
      - name: Setup Python
        uses: actions/setup-python@v5
      - uses: astral-sh/setup-uv@v4
        with:
          version: "latest"
      - name: Install dependencies
        run: |
          uv sync
      - name: Build
        run: |
          cd _docs
          uv run make html
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v2
        with:
          path: _docs/_build/html

  # Deployment job
  deploy-docs:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    needs: build-docs
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v3