name: Publish

on:
  release:
    types: [published]

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

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

      - name: Set version from tag
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          sed -i "s/^version = \".*\"/version = \"${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:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v5

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

      - 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: |
          VERSION="${GITHUB_REF_NAME#v}"
          gh release upload "${GITHUB_REF_NAME}" \
            "cli/claude-finance-kit-${VERSION}.zip" \
            --clobber

  publish-npm:
    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 from tag
        working-directory: cli
        run: |
          VERSION="${GITHUB_REF_NAME#v}"
          npm version "${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 }}
