name: CI

on:
  push:
    branches:
      - main
  pull_request:

concurrency:
  group: ${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  # Make sure commit messages follow the conventional commits convention:
  # https://www.conventionalcommits.org
  commitlint:
    name: Lint Commit Messages
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - uses: wagoid/commitlint-github-action@v6.2.1
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: actions/setup-python@v6
        with:
          python-version: "3.11"
      - uses: pre-commit/action@v3.0.1

  test:
    strategy:
      fail-fast: false
      matrix:
        python-version:
          - "3.11"
          - "3.14"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Set up uv
        uses: astral-sh/setup-uv@v7
        with:
          python-version: ${{ matrix.python-version }}
          activate-environment: true
      - run: uv pip install pip
      - name: Test with Pytest
        run:  uv run pytest --log-cli-level=DEBUG -vv -s
        shell: bash

  build:
    name: Build Package
    runs-on: ubuntu-latest
    if: github.ref != 'refs/heads/main'
    steps:
      - uses: actions/checkout@v6
      - name: Set up uv
        uses: astral-sh/setup-uv@v7
        with:
          python-version: "3.11"
          activate-environment: true
      - name: Build package
        run: uv build

  # Test semantic-release configuration on PR branches
  test-release:
    name: Test Semantic Release
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
      - name: Setup Git for Semantic Release Testing
        run: |
          # Setup a temp branch to test
          git checkout -B temp-main-branch
          git merge ${{ github.event.pull_request.head.sha }} --no-edit
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
      - name: Test Semantic Release (No-op)
        id: test-release
        uses: python-semantic-release/python-semantic-release@v10.5.2
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          changelog: true
          # Use noop mode to test without making changes
          no_operation_mode: true
      - name: Test Semantic Release (Dry Run)
        id: test-release-dry
        uses: python-semantic-release/python-semantic-release@v10.5.2
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          changelog: true
          # Use dry run mode to test without committing
          commit: false
          tag: false
          push: false
          vcs_release: false
  release:
    runs-on: ubuntu-latest
    needs:
      - test
    concurrency: release
    if: github.ref == 'refs/heads/main'
    permissions:
      contents: write
      issues: write
      pull-requests: write
      id-token: write
      actions: write
      packages: write
    environment:
      name: release

    steps:
    - uses: actions/checkout@v6
      with:
        fetch-depth: 0
        persist-credentials: false
    - name: Python Semantic Release
      id: release
      uses: python-semantic-release/python-semantic-release@v10.5.2
      with:
        github_token: ${{ secrets.GH_TOKEN }}
        changelog: true

    - name: Publish package distributions to PyPI
      uses: pypa/gh-action-pypi-publish@v1.13.0
      # NOTE: DO NOT wrap the conditional in ${{ }} as it will always evaluate to true.
      # See https://github.com/actions/runner/issues/1173
      if: steps.release.outputs.released == 'true'
      with:
        packages-dir: dist
        print-hash: true
        verbose: true

    - name: Publish package distributions to GitHub Releases
      uses: python-semantic-release/publish-action@v10.5.2
      if: steps.release.outputs.released == 'true'
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        tag: ${{ steps.release.outputs.tag }}
