name: Release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version type (patch, minor, major)'
        required: true
        default: 'patch'
        type: choice
        options:
          - patch
          - minor
          - major

jobs:
  release:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'

      - name: Configure Git
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"

      - name: Install dependencies
        run: |
          cd easy-obsidian-mcp
          npm ci

      - name: Build
        run: |
          cd easy-obsidian-mcp
          npm run build

      - name: Bump version
        id: version
        run: |
          cd easy-obsidian-mcp
          NEW_VERSION=$(npm version ${{ github.event.inputs.version }} --no-git-tag-version)
          echo "new_version=${NEW_VERSION:1}" >> $GITHUB_OUTPUT
          echo "Bumped to version ${NEW_VERSION}"

      - name: Update create script version
        run: |
          cd create-easy-obsidian-mcp
          npm version ${{ steps.version.outputs.new_version }} --no-git-tag-version || true

      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v5
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: "chore: release v${{ steps.version.outputs.new_version }}"
          title: "Release v${{ steps.version.outputs.new_version }}"
          body: |
            ## 🚀 Release v${{ steps.version.outputs.new_version }}
            
            This PR was automatically created by the release workflow.
            
            ### Changes
            - Bumped version to ${{ steps.version.outputs.new_version }}
            - Updated package files
            
            ### Next Steps
            1. Review the changes
            2. Merge this PR to trigger automatic npm publish
            
            The npm publish will happen automatically when this PR is merged to main.
          branch: release/v${{ steps.version.outputs.new_version }}
          delete-branch: true
          labels: |
            release
            automated