---
name: Release

on:
  workflow_dispatch:
    inputs:
      title:
        description: "Release title defaults to the semantic version"
        required: false
      bump:
        description: "Bump type (major/minor/patch) defaults to auto"
        default: "auto"
        required: true

env:
  MAIN_PY_VER: "3.13"

jobs:
  release:
    runs-on: ubuntu-latest
    environment: deployment
    name: Build, publish, and release
    steps:
      - name: Checkout sources
        uses: actions/checkout@v5
        with:
          fetch-depth: 0
          ssh-key: ${{ secrets.DEPLOY_KEY }}

      - name: Setup Python
        uses: actions/setup-python@v6
        with:
          python-version: ${{ env.MAIN_PY_VER }}

      - name: Cache pip repository
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}-${{ env.MAIN_PY_VER }}

      - name: Prepare python environment
        run: |
          pip install -r requirements.txt
          poetry config virtualenvs.create true
          poetry config virtualenvs.in-project true

      - name: Cache poetry virtual environment
        uses: actions/cache@v4
        with:
          path: .venv
          key: ${{ runner.os }}-poetry-${{ hashFiles('**/pyproject.toml') }}-${{ env.MAIN_PY_VER }}

      - name: Configure git
        run: |
          git config user.name "${{ github.actor }}"
          git config user.email "${{ github.actor }}@users.noreply.github.com"

      - name: Determine version and create changelog
        id: bumper
        uses: tomerfi/version-bumper-action@2.0.5
        with:
          bump: '${{ github.event.inputs.bump }}'

      - name: Set new project version
        uses: sandstromviktor/toml-editor@2.0.0
        with:
          file: pyproject.toml
          key: project.version
          value: ${{ steps.bumper.outputs.next }}

      - name: Commit, tag, and push
        run: |
          git add pyproject.toml
          git commit -m "build: bump version to ${{ steps.bumper.outputs.next }} [skip ci]"
          git push
          git tag ${{ steps.bumper.outputs.next }} -m "${{ steps.bumper.outputs.next }}"
          git push origin ${{ steps.bumper.outputs.next }}

      - name: Verify documentation site build
        run: |
          poetry lock
          poetry install --no-interaction
          poetry run poe docs_build

      - name: Publish build to PyPi
        run: |
          rm -rf ./dist
          poetry publish --build --no-interaction -u __token__ -p ${{ secrets.PYPI_TOKEN }}

      - name: Set development project version
        uses: sandstromviktor/toml-editor@2.0.0
        with:
          file: pyproject.toml
          key: project.version
          value: ${{ steps.bumper.outputs.dev }}

      - name: Commit and push
        run: |
          git add pyproject.toml
          git commit -m "build: bump version to ${{ steps.bumper.outputs.dev }} [skip ci]"
          git push

      - name: Create a release name
        id: release_name
        uses: actions/github-script@v8
        with:
          script: |
            var retval = '${{ steps.bumper.outputs.next }}'
            if ('${{ github.event.inputs.title }}') {
              retval = retval.concat(' - ${{ github.event.inputs.title }}')
            }
            core.setOutput('value', retval)

      - name: Create a release
        id: gh_release
        uses: actions/github-script@v8
        with:
          github-token: ${{ secrets.RELEASE_PAT }}
          script: |
            const repo_name = context.payload.repository.full_name
            const response = await github.request('POST /repos/' + repo_name + '/releases', {
              tag_name: '${{ steps.bumper.outputs.next }}',
              name: '${{ steps.release_name.outputs.value }}',
              generate_release_notes: true
            })
            core.setOutput('html_url', response.data.html_url)
