name: "Manual Release"

on:
  workflow_dispatch:
    inputs:
      version_type:
        description: 'Version bump type'
        required: true
        type: choice
        options:
          - patch
          - minor
          - major
        default: 'patch'

# Minimal permissions at workflow level - each job specifies what it needs
permissions: {}

jobs:
  create-release:
    name: Create release tag
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      new_version: ${{ steps.version.outputs.new_version }}
      current_version: ${{ steps.version.outputs.current_version }}
    steps:
      - name: Generate GitHub App token
        uses: actions/create-github-app-token@f8d387b68d61c58ab83c6c016672934102569859 # v3.0.0
        id: app-token
        with:
          app-id: ${{ vars.APP_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}

      - name: Get GitHub App User ID
        id: get-user-id
        run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}

      - name: Checkout
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
          token: ${{ steps.app-token.outputs.token }}

      - name: Set up Node.js
        uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
        with:
          node-version: '24.14.0'

      - name: Configure git
        run: |
          git config --local user.name "${{ steps.app-token.outputs.app-slug }}[bot]"
          git config --local user.email "${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com"

      - name: Calculate new version
        id: version
        run: |
          CURRENT_VERSION=$(node -p "require('./package.json').version")
          echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT

          IFS='.' read -r -a version_parts <<< "$CURRENT_VERSION"
          major="${version_parts[0]}"
          minor="${version_parts[1]}"
          patch="${version_parts[2]}"

          case "${{ github.event.inputs.version_type }}" in
            "patch")
              patch=$((patch + 1))
              ;;
            "minor")
              minor=$((minor + 1))
              patch=0
              ;;
            "major")
              major=$((major + 1))
              minor=0
              patch=0
              ;;
          esac

          NEW_VERSION="v${major}.${minor}.${patch}"
          echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
          echo "### 📦 Release Information" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "- **Current version:** $CURRENT_VERSION" >> $GITHUB_STEP_SUMMARY
          echo "- **New version:** $NEW_VERSION" >> $GITHUB_STEP_SUMMARY
          echo "- **Bump type:** ${{ github.event.inputs.version_type }}" >> $GITHUB_STEP_SUMMARY

      - name: Update package.json
        run: |
          npm version ${{ github.event.inputs.version_type }} --no-git-tag-version

      - name: Commit and tag
        run: |
          git add package.json package-lock.json
          git commit -m "chore: release ${{ steps.version.outputs.new_version }}"
          git tag -a "${{ steps.version.outputs.new_version }}" -m "Release ${{ steps.version.outputs.new_version }}"

      - name: Push changes
        run: |
          git push origin main
          git push origin ${{ steps.version.outputs.new_version }}

      - name: Add summary
        run: |
          echo "### ✅ Release Created Successfully" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Tag:** \`${{ steps.version.outputs.new_version }}\` has been pushed" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "The release workflow will now be triggered automatically." >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "**Next steps:**" >> $GITHUB_STEP_SUMMARY
          echo "1. Monitor the release: [GitHub Actions](https://github.com/${{ github.repository }}/actions)" >> $GITHUB_STEP_SUMMARY
          echo "2. Check the release page: [Releases](https://github.com/${{ github.repository }}/releases/tag/${{ steps.version.outputs.new_version }})" >> $GITHUB_STEP_SUMMARY
          echo "3. Verify NPM publish: [NPM Package](https://www.npmjs.com/package/@kubeasy-dev/kubeasy-cli)" >> $GITHUB_STEP_SUMMARY
