name: Auto Release & Publish

on:
  pull_request:
    types: [closed]
    branches: [main]

jobs:
  release:
    if: github.event.pull_request.merged == true
    runs-on: ubuntu-latest
    permissions:
      contents: write
    outputs:
      version: ${{ steps.bump.outputs.version }}
      tag: ${{ steps.bump.outputs.tag }}
    steps:
      - uses: actions/checkout@v5
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: Get latest tag version
        id: current
        run: |
          LATEST_TAG=$(git tag -l 'v*' --sort=-v:refname | head -1)
          if [ -z "$LATEST_TAG" ]; then
            LATEST_TAG="v0.0.0"
          fi
          VERSION="${LATEST_TAG#v}"
          echo "version=$VERSION" >> "$GITHUB_OUTPUT"
          echo "tag=$LATEST_TAG" >> "$GITHUB_OUTPUT"

      - name: Bump patch version
        id: bump
        run: |
          CURRENT="${{ steps.current.outputs.version }}"
          IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT"
          PATCH=$((PATCH + 1))
          NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}"
          echo "version=$NEW_VERSION" >> "$GITHUB_OUTPUT"
          echo "tag=v${NEW_VERSION}" >> "$GITHUB_OUTPUT"

      - name: Generate changelog
        id: changelog
        run: |
          PREV_TAG="${{ steps.current.outputs.tag }}"
          if [ "$PREV_TAG" = "v0.0.0" ]; then
            LOG=$(git log --pretty=format:"- %s (%h)" --no-merges)
          else
            LOG=$(git log "${PREV_TAG}..HEAD" --pretty=format:"- %s (%h)" --no-merges)
          fi
          {
            echo "body<<EOF"
            echo "## What's Changed"
            echo ""
            echo "$LOG"
            echo ""
            if [ "$PREV_TAG" != "v0.0.0" ]; then
              echo "**Full Changelog**: https://github.com/${{ github.repository }}/compare/${PREV_TAG}...${{ steps.bump.outputs.tag }}"
            fi
            echo "EOF"
          } >> "$GITHUB_OUTPUT"

      - name: Create release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create "${{ steps.bump.outputs.tag }}" \
            --target main \
            --title "v${{ steps.bump.outputs.version }}" \
            --notes "${{ steps.changelog.outputs.body }}"

  publish-pypi:
    needs: release
    runs-on: ubuntu-latest
    environment: release
    permissions:
      id-token: write
    steps:
      - uses: actions/checkout@v5

      - uses: actions/setup-python@v6
        with:
          python-version: "3.12"

      - name: Set version
        run: |
          sed -i "s/^version = \".*\"/version = \"${{ needs.release.outputs.version }}\"/" pyproject.toml

      - name: Build
        run: |
          pip install build
          python -m build --wheel --sdist

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

  package-plugin:
    needs: release
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v5

      - uses: actions/setup-node@v5
        with:
          node-version: "20"

      - name: Set version
        working-directory: cli
        run: |
          npm version "${{ needs.release.outputs.version }}" --no-git-tag-version --allow-same-version

      - name: Install and build CLI
        working-directory: cli
        run: |
          npm ci
          npm run build

      - name: Package plugin
        working-directory: cli
        run: npm run package

      - name: Upload plugin to release
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release upload "${{ needs.release.outputs.tag }}" \
            "cli/claude-finance-kit-${{ needs.release.outputs.version }}.zip" \
            --clobber

  publish-npm:
    needs: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5

      - uses: actions/setup-node@v5
        with:
          node-version: "20"
          registry-url: "https://registry.npmjs.org"

      - name: Set version
        working-directory: cli
        run: |
          npm version "${{ needs.release.outputs.version }}" --no-git-tag-version --allow-same-version

      - name: Install and build CLI
        working-directory: cli
        run: |
          npm ci
          npm run build

      - name: Publish to npm
        working-directory: cli
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
