name: Build Wheels

on:
  workflow_dispatch:
    inputs:
      requested_release_tag:
        description: "The tag to use for this release (e.g., `v2.3.1`)"
        required: true

jobs:
  sanity_check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - uses: actions/setup-python@v5
        name: Install Python
        with:
          python-version: "3.13"

      - run: |
          pip install packaging

      - name: Normalize the release version
        run: |
          echo "release_version=`echo '${{ github.event.inputs.requested_release_tag }}' | sed 's/^v//'`" >> $GITHUB_ENV

      - name: Normalize the release tag
        run: |
          echo "release_tag=v${release_version}" >> $GITHUB_ENV

      - name: Get the VERSION from setup.py
        run: |
          echo "ciso8601_version=`grep -Po 'VERSION = "\K[^"]*' setup.py`" >> $GITHUB_ENV

      - name: Get the latest version from PyPI
        run: |
          curl https://pypi.org/pypi/ciso8601/json | python -c 'import json, sys; contents=sys.stdin.read(); parsed = json.loads(contents); print("pypi_version=" + parsed["info"]["version"])' >> $GITHUB_ENV

      - name: Log all the things
        run: |
          echo 'Requested release tag `${{ github.event.inputs.requested_release_tag }}`'
          echo 'Release version       `${{ env.release_version }}`'
          echo 'Release tag           `${{ env.release_tag }}`'
          echo 'VERSION in setup.py   `${{ env.ciso8601_version }}`'
          echo 'Version in PyPI       `${{ env.pypi_version }}`'

      - name: Verify that the version string we produced looks like a version string
        run: |
          echo "${{ env.release_version }}" | sed '/^[0-9]\+\.[0-9]\+\.[0-9]\+$/!{q1}'

      - name: Verify that the version tag we produced looks like a version tag
        run: |
          echo "${{ env.release_tag }}" | sed '/^v[0-9]\+\.[0-9]\+\.[0-9]\+$/!{q1}'

      - name: Verify that the release version matches the VERSION in setup.py
        run: |
          [[ ${{ env.release_version }} == ${{ env.ciso8601_version }} ]]

      - name: Verify that the `release_version` is larger/newer than the existing release in PyPI
        run: |
          python -c 'import sys; from packaging import version; code = 0 if version.parse("${{ env.pypi_version }}") < version.parse("${{ env.release_version }}") else 1; sys.exit(code)'

      - name: Verify that the `release_version` is present in the CHANGELOG
        # TODO: Use something like `changelog-cli` to extract the correct version number
        run: |
          grep ${{ env.release_version }} CHANGELOG.md

      - name: Serialize normalized release values
        run: |
          echo -e "release_version=${{ env.release_version }}\nrelease_tag=${{ env.release_tag }}" > release_values.txt

      - name: Share normalized release values
        uses: actions/upload-artifact@v4
        with:
          name: release_values
          path: release_values.txt

  build_wheels:
    name: Build wheel on ${{ matrix.os }}
    needs: [sanity_check]
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]

    steps:
      - uses: actions/checkout@v5

      - name: Set up QEMU # Needed to build aarch64 wheels
        if: runner.os == 'Linux'
        uses: docker/setup-qemu-action@v3
        with:
          platforms: all

      - uses: closeio/cibuildwheel@v3.1.4

      - uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.os }}-wheels
          path: ./wheelhouse/*.whl

  build_sdist:
    name: Build source distribution
    needs: [sanity_check]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - uses: actions/setup-python@v5
        name: Install Python
        with:
          python-version: "3.13"

      - name: Get build tool
        run: pip install --upgrade build

      - name: Build sdist
        run: python -m build

      - uses: actions/upload-artifact@v4
        with:
          name: sdist
          path: dist/*.tar.gz
