name: Prepare release
run-name: Prepare release ${{ inputs.next_version }}

on:
  workflow_dispatch:
    inputs:
      next_version:
        required: true
        type: string
        description: "Version name"

permissions:
  contents: write
  pull-requests: write

jobs:
  prepare_release:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683

      - uses: ./.github/actions/dangerous-git-checkout

      - name: Configure git
        run: |
          git config --local user.email "github-actions@github.com"
          git config --local user.name "GitHub Actions"

      - name: Setup Node.js 20.x
        uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af
        with:
          node-version: 20.x

      - name: Install pnpm
        uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2

      - name: Install dependencies
        run: pnpm install --config.platform=linux --config.architecture=x64

      - name: Bump version
        run: |
          cd apps/web
          pnpm version ${{ inputs.next_version }} --no-workspaces-update

      - name: Commit changes and create a branch
        run: |
          branch_name="release-v${{ inputs.next_version }}"
          git checkout -b "$branch_name"
          git add .
          git commit -m "chore: release v${{ inputs.next_version }}"
          git push origin "$branch_name"

      - name: Create pull request
        run: |
          gh pr create \
            --base main \
            --head "release-v${{ inputs.next_version }}" \
            --title "chore: bump version to v${{ inputs.next_version }}" \
            --body "This PR contains the changes for the v${{ inputs.next_version }} release."
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
