name: Release

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      publish_to_testpypi:
        description: 'Publish to TestPyPI'
        type: boolean
        default: true
      publish_to_crates_io:
        description: 'Publish to Crates.io'
        type: boolean
        default: false

permissions:
  contents: read

jobs:
  # Build wheels for Linux (x86_64 and ARM64)
  linux:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: [x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --find-interpreter
          sccache: 'true'
          manylinux: auto
        env:
          CFLAGS_aarch64_unknown_linux_gnu: "-D__ARM_ARCH=8"
          TARGET_CC: "clang"
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-linux-${{ matrix.target }}
          path: dist

  # Build wheels for Windows
  windows:
    runs-on: windows-latest
    strategy:
      matrix:
        target: [x86_64-pc-windows-msvc]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --find-interpreter
          sccache: 'true'
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-windows-${{ matrix.target }}
          path: dist

  # Build wheels for macOS (x86_64 and ARM64)
  macos:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - target: x86_64-apple-darwin
            os: macos-15-intel
          - target: aarch64-apple-darwin
            os: macos-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist -i python
          sccache: 'true'
      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-macos-${{ matrix.target }}
          path: dist

  # Build source distribution
  sdist:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Build sdist
        uses: PyO3/maturin-action@v1
        with:
          command: sdist
          args: --out dist
      - name: Upload sdist
        uses: actions/upload-artifact@v4
        with:
          name: wheels-sdist
          path: dist

  publish-testpypi:
    name: Publish to TestPyPI
    runs-on: ubuntu-latest
    needs: [linux, windows, macos, sdist]
    if: github.event_name == 'workflow_dispatch' && inputs.publish_to_testpypi == true
    environment:
      name: testpypi
      url: https://test.pypi.org/p/json-register-rust
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: wheels-*
          merge-multiple: true
          path: dist

      - name: Publish to TestPyPI
        uses: PyO3/maturin-action@v1
        env:
          MATURIN_PYPI_TOKEN: ${{ secrets.TEST_PYPI_API_TOKEN }}
          MATURIN_REPOSITORY_URL: https://test.pypi.org/legacy/
        with:
          command: upload
          args: --non-interactive --skip-existing dist/*

  publish-crates-io:
    name: Publish to Crates.io
    runs-on: ubuntu-latest
    if: github.event_name == 'workflow_dispatch' && inputs.publish_to_crates_io == true
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - name: Publish to Crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish
