name: Pre-release

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Pre-release version (e.g. 0.1.0-alpha.1, 0.2.0-rc.1)"
        required: true
        type: string

env:
  CARGO_TERM_COLOR: always

jobs:
  pre-release:
    name: Publish pre-release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Validate version format
        run: |
          if ! echo "${{ inputs.version }}" | grep -qP '^\d+\.\d+\.\d+-(alpha|beta|rc)\.\d+$'; then
            echo "::error::Invalid version format '${{ inputs.version }}'. Expected: X.Y.Z-(alpha|beta|rc).N"
            exit 1
          fi

      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2

      - name: Set version in Cargo.toml
        run: sed -i 's/^version = ".*"/version = "${{ inputs.version }}"/' Cargo.toml

      - name: Verify build
        run: cargo build --all-features

      - name: Run tests
        run: cargo test --all-features

      - name: Publish to crates.io
        run: cargo publish --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Create git tag
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "v${{ inputs.version }}" -m "Pre-release v${{ inputs.version }}"
          git push origin "v${{ inputs.version }}"

      - name: Create GitHub pre-release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create "v${{ inputs.version }}" \
            --title "v${{ inputs.version }}" \
            --generate-notes \
            --prerelease
