name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      version:
        description: 'Version tag to simulate (e.g., v0.1.0-test)'
        required: true
        default: 'v0.1.0-test'

permissions:
  contents: read

concurrency:
  group: release
  cancel-in-progress: false

jobs:
  # Validate tag matches Cargo.toml and run tests
  validate:
    name: Validate & test
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable

      - uses: Swatinem/rust-cache@v2

      - name: Check version matches tag
        if: github.event_name == 'push'
        run: |
          TAG_VERSION="${GITHUB_REF#refs/tags/v}"
          CARGO_VERSION=$(sed -n 's/^version = "\(.*\)"/\1/p' Cargo.toml | head -1)
          echo "Tag version:   $TAG_VERSION"
          echo "Cargo version: $CARGO_VERSION"
          if [ "$TAG_VERSION" != "$CARGO_VERSION" ]; then
            echo "::error::Tag v$TAG_VERSION does not match Cargo.toml version $CARGO_VERSION"
            exit 1
          fi

      - name: Run tests
        run: cargo test

  # Build wheels for multiple platforms
  build-wheels:
    name: Build wheels (${{ matrix.os }})
    needs: validate
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-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:
          command: build
          args: --release --out dist -i python

      - name: Smoke test wheel
        shell: bash
        run: |
          pip install dist/*.whl
          cd /tmp
          python -c "from webspec_index import query, search, exists; print('Import OK')"
          webspec-index --version

      - name: Upload wheels
        uses: actions/upload-artifact@v4
        with:
          name: wheels-${{ matrix.os }}
          path: dist
          retention-days: 5

  # Build source distribution
  build-sdist:
    name: Build source distribution
    needs: validate
    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: sdist
          path: dist
          retention-days: 5

  # Publish to PyPI using Trusted Publishers (OIDC)
  publish:
    name: Publish to PyPI
    needs: [build-wheels, build-sdist]
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    environment:
      name: pypi
      url: https://pypi.org/project/webspec-index/
    permissions:
      id-token: write
      contents: read

    steps:
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: "*"
          merge-multiple: true
          path: dist

      - name: List distributions
        run: ls -lh dist/

      - uses: astral-sh/setup-uv@v5

      - name: Publish to PyPI
        run: uv publish dist/*

  # Create GitHub Release
  github-release:
    name: Create GitHub Release
    needs: publish
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          pattern: "*"
          merge-multiple: true
          path: dist

      - name: Extract version from tag
        id: version
        run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT

      - name: Create Release
        uses: softprops/action-gh-release@v2
        with:
          name: Release v${{ steps.version.outputs.VERSION }}
          generate_release_notes: true
          body: |
            ## Installation

            ```bash
            pip install webspec-index==${{ steps.version.outputs.VERSION }}
            ```

            Or use with uvx:

            ```bash
            uvx webspec-index@${{ steps.version.outputs.VERSION }} --help
            ```
          files: dist/*
          draft: false
          prerelease: false
