name: Release

on:
  release:
    types: [published]

env:
  DEFAULT_PYTHON: 3.11

jobs:
  build:
    name: Build PyPI package
    runs-on: ubuntu-latest

    permissions:
      contents: read

    steps:
      - name: Checkout the repository
        uses: actions/checkout@v6.0.1
        with:
          ref: ${{ github.event.release.target_commitish }}

      - name: Set up UV and Python
        uses: astral-sh/setup-uv@v7
        with:
          python-version: ${{ env.DEFAULT_PYTHON }}
          enable-cache: true
          cache-dependency-glob: "uv.lock"

      - name: Install the project
        run: uv sync --all-groups

      - name: Run tests
        run: |
          uv run pytest

      - name: Build package
        run: |
          uv build

      - name: Store the distribution packages
        uses: actions/upload-artifact@v5
        with:
          name: python-package-distributions
          path: dist/

  publish:
    name: Publish to PyPI 📦
    needs:
      - build
    runs-on: ubuntu-latest

    permissions:
      id-token: write

    environment:
      name: pypi
      url: https://pypi.org/p/asusrouter

    steps:
      - name: Download all the dists
        uses: actions/download-artifact@v6
        with:
          name: python-package-distributions
          path: dist/
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
