name: CI

on:
  push:
    branches:
      - main
    tags:
      - "*"
  pull_request:
    branches:
      - main
  schedule:
    - cron: '0 0 * * 1'
  workflow_dispatch:

defaults:
  run:
    shell: bash

env:
  FORCE_COLOR: true

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  build:
    name: Build & verify package
    runs-on: ubuntu-latest

    permissions:
      attestations: write
      id-token: write

    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - uses: hynek/build-and-inspect-python-package@v2
        with:
          attest-build-provenance-github: ${{ github.event_name != 'pull_request' }}

  tox:
    name: Tests on ${{ matrix.os }}
    needs: build
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ['ubuntu-latest', 'macos-latest', 'windows-latest']

    steps:
      - uses: astral-sh/setup-uv@v6
      - name: Download packages
        uses: actions/download-artifact@v4
        with:
          name: Packages
          path: dist/
      - run: tar xf dist/*.tar.gz --strip-components=1
      - run: uv sync

      - name: Install tox
        run: |
          uv tool install -p 3.13 --with=tox-uv tox
      - name: Show tox config
        run: tox c
      - name: Run tox
        run: tox -v --exit-and-dump-after 60 --parallel-no-spinner --installpkg dist/*.whl

      - uses: codecov/codecov-action@v5
        if: ${{ always() }}
        with:
          token: ${{ secrets.CODECOV_TOKEN }}

  publish:
    runs-on: ubuntu-latest
    environment: "Package deployment"
    needs: [tox]
    permissions:
      attestations: write
      id-token: write
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
    steps:
      - name: Download packages
        uses: actions/download-artifact@v4
        with:
          name: Packages
          path: dist/
      - uses: pypa/gh-action-pypi-publish@release/v1
