name: Release

on:
  workflow_dispatch:
  push:
    tags:
      - "v*"

jobs:
  # ========================================================================
  # Build Python wheels for PyPI (Linux, macOS, Windows × x86_64/aarch64)
  # ========================================================================
  wheels-linux:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        target: [x86_64, aarch64]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.x"
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          manylinux: auto
          args: --release --out dist --interpreter 3.10 3.11 3.12 3.13
      - uses: actions/upload-artifact@v4
        with:
          name: wheels-linux-${{ matrix.target }}
          path: dist

  wheels-macos:
    runs-on: macos-14
    strategy:
      matrix:
        target: [x86_64, aarch64]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.x"
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: ${{ matrix.target }}
          args: --release --out dist --interpreter 3.10 3.11 3.12 3.13
      - uses: actions/upload-artifact@v4
        with:
          name: wheels-macos-${{ matrix.target }}
          path: dist

  wheels-windows:
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.x"
          architecture: x64
      - name: Build wheels
        uses: PyO3/maturin-action@v1
        with:
          target: x64
          args: --release --out dist --interpreter 3.10 3.11 3.12 3.13
      - uses: actions/upload-artifact@v4
        with:
          name: wheels-windows-x64
          path: dist

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

  # ========================================================================
  # Build native CLI binaries for GitHub Releases
  # ========================================================================
  cli-binaries:
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            name: etoon-linux-x86_64
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            name: etoon-linux-aarch64
            cross: true
          - target: x86_64-apple-darwin
            os: macos-14
            name: etoon-macos-x86_64
          - target: aarch64-apple-darwin
            os: macos-14
            name: etoon-macos-aarch64
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            name: etoon-windows-x86_64.exe
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      - name: Install cross (for aarch64 Linux)
        if: matrix.cross
        run: cargo install cross --locked
      - name: Build (native)
        if: '!matrix.cross'
        run: cargo build --release --bin etoon --no-default-features --target ${{ matrix.target }}
      - name: Build (cross)
        if: matrix.cross
        run: cross build --release --bin etoon --no-default-features --target ${{ matrix.target }}
      - name: Rename binary
        shell: bash
        run: |
          if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
            cp target/${{ matrix.target }}/release/etoon.exe ${{ matrix.name }}
          else
            cp target/${{ matrix.target }}/release/etoon ${{ matrix.name }}
          fi
      - uses: actions/upload-artifact@v4
        with:
          name: cli-${{ matrix.target }}
          path: ${{ matrix.name }}

  # ========================================================================
  # Publish to PyPI (on tag push)
  # ========================================================================
  publish-pypi:
    if: startsWith(github.ref, 'refs/tags/v')
    needs: [wheels-linux, wheels-macos, wheels-windows, sdist]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: "wheels-*"
          path: dist
          merge-multiple: true
      - uses: actions/download-artifact@v4
        with:
          name: sdist
          path: dist
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_TOKEN }}
          skip-existing: true

  # ========================================================================
  # Publish to crates.io (on tag push)
  # ========================================================================
  publish-crates:
    if: startsWith(github.ref, 'refs/tags/v')
    runs-on: ubuntu-latest
    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 --no-default-features

  # ========================================================================
  # Create GitHub Release with CLI binaries (on tag push)
  # ========================================================================
  github-release:
    if: startsWith(github.ref, 'refs/tags/v')
    needs: [cli-binaries]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/download-artifact@v4
        with:
          pattern: "cli-*"
          path: cli
          merge-multiple: true
      - name: Create release
        uses: softprops/action-gh-release@v1
        with:
          files: cli/*
          draft: false
          prerelease: false
          generate_release_notes: true
