name: Prepare Release

on:
  workflow_dispatch:
    inputs:
      bump:
        description: Version bump type (ignored if explicit version is provided)
        required: true
        default: patch
        type: choice
        options:
          - patch
          - minor
          - major
      version:
        description: Explicit version (e.g. 1.3.0). Leave empty to use bump.
        required: false
        default: ""

concurrency:
  group: release-${{ github.ref }}
  cancel-in-progress: false

jobs:
  release:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    permissions:
      contents: write
    outputs:
      tag: ${{ steps.prep.outputs.tag }}

    steps:
      - name: Checkout
        uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          fetch-depth: 0

      - name: Setup Python
        uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
        with:
          python-version: "3.12"

      - name: Prepare release files
        id: prep
        run: |
          python scripts/prepare_release.py \
            --bump "${{ inputs.bump }}" \
            --version "${{ inputs.version }}" \
            --github-output "$GITHUB_OUTPUT"

      - name: Build local sdist for packaging hashes
        run: |
          python -m pip install --upgrade pip build
          python -m build --sdist

      - name: Prepare Windows packaging artifacts
        id: windows_packaging
        run: |
          python scripts/prepare_windows_packaging.py \
            --version "${{ steps.prep.outputs.version }}" \
            --repo "${{ github.repository }}" \
            --sdist-file "dist/lore_book-${{ steps.prep.outputs.version }}.tar.gz" \
            --github-output "$GITHUB_OUTPUT"

      - name: Validate tag does not already exist
        run: |
          if git rev-parse -q --verify "refs/tags/${{ steps.prep.outputs.tag }}" >/dev/null; then
            echo "Tag already exists locally: ${{ steps.prep.outputs.tag }}"
            exit 1
          fi
          if git ls-remote --tags origin "refs/tags/${{ steps.prep.outputs.tag }}" | grep -q .; then
            echo "Tag already exists on origin: ${{ steps.prep.outputs.tag }}"
            exit 1
          fi

      - name: Commit version and changelog
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add src/lore/__init__.py CHANGELOG.md packaging/scoop/lore-book.json packaging/winget/submission-${{ steps.prep.outputs.version }}.md
          git commit -m "chore(release): ${{ steps.prep.outputs.version }}"
          git tag "${{ steps.prep.outputs.tag }}"
          git push origin HEAD
          git push origin "${{ steps.prep.outputs.tag }}"

      - name: Create GitHub release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create "${{ steps.prep.outputs.tag }}" \
            --title "${{ steps.prep.outputs.tag }}" \
            --notes-file .release-notes.md

  publish:
    needs: release
    runs-on: ubuntu-latest
    timeout-minutes: 15
    environment: pypi
    permissions:
      id-token: write  # required for trusted publishing
      contents: read

    steps:
      - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4
        with:
          ref: ${{ needs.release.outputs.tag }}

      - uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
        with:
          python-version: "3.12"

      - name: Install build
        run: pip install build

      - name: Build distributions
        run: python -m build

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@ed0c53931b1dc9bd32cbe73a98c7f6766f8a527e # release/v1
