name: Publish to PyPI

on:
  push:
    tags:
      - "sdk-v*"       # triggers SDK-only publish  →  peaky-peek
      - "server-v*"    # triggers server publish    →  peaky-peek-server
      - "v*"           # triggers both

permissions:
  contents: read
  id-token: write  # required for PyPI trusted publishing (OIDC)

jobs:
  # ─────────────────────────────────────────────
  # peaky-peek  (SDK only)
  # ─────────────────────────────────────────────
  publish-sdk:
    name: Publish peaky-peek (SDK)
    runs-on: ubuntu-latest
    if: startsWith(github.ref_name, 'sdk-v') || startsWith(github.ref_name, 'v')
    environment:
      name: pypi-sdk
      url: https://pypi.org/project/peaky-peek/

    steps:
      - uses: actions/checkout@v4

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

      - name: Install build tools
        run: pip install build

      - name: Derive release version from tag
        run: |
          tag="${GITHUB_REF_NAME}"
          version="${tag#sdk-v}"
          version="${version#v}"
          echo "RELEASE_VERSION=${version}" >> "$GITHUB_ENV"

      - name: Set SDK version from tag
        run: |
          python3 - <<'PY'
          import os
          import re
          from pathlib import Path

          version = os.environ["RELEASE_VERSION"]
          path = Path("pyproject.toml")
          content = path.read_text(encoding="utf-8")
          updated, count = re.subn(
              r'(?m)^version = "[^"]+"$',
              f'version = "{version}"',
              content,
              count=1,
          )
          if count != 1:
              raise SystemExit("failed to update SDK version in pyproject.toml")
          path.write_text(updated, encoding="utf-8")
          PY

      - name: Show SDK build metadata
        run: |
          python3 - <<'PY'
          import tomllib
          with open("pyproject.toml", "rb") as f:
              data = tomllib.load(f)
          print(data["project"]["name"], data["project"]["version"])
          PY

      - name: Build peaky-peek wheel + sdist
        run: python -m build
        # pyproject.toml at root already targets peaky-peek / agent_debugger_sdk only

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          skip-existing: true
        # No token needed — uses OIDC trusted publishing.
        # Configure on PyPI: https://pypi.org/manage/account/publishing/
        #   Publisher: GitHub Actions
        #   Owner:     acailic
        #   Repo:      agent_debugger
        #   Workflow:  publish.yml
        #   Env name:  pypi-sdk

  # ─────────────────────────────────────────────
  # peaky-peek-server  (full stack)
  # ─────────────────────────────────────────────
  publish-server:
    name: Publish peaky-peek-server
    runs-on: ubuntu-latest
    if: startsWith(github.ref_name, 'server-v') || startsWith(github.ref_name, 'v')
    environment:
      name: pypi-server
      url: https://pypi.org/project/peaky-peek-server/

    steps:
      - uses: actions/checkout@v4

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

      - uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: npm
          cache-dependency-path: frontend/package-lock.json

      - name: Install build tools
        run: pip install build

      - name: Derive release version from tag
        run: |
          tag="${GITHUB_REF_NAME}"
          version="${tag#server-v}"
          version="${version#v}"
          echo "RELEASE_VERSION=${version}" >> "$GITHUB_ENV"

      - name: Swap in server pyproject for build
        run: cp pyproject-server.toml pyproject.toml

      - name: Build frontend bundle
        working-directory: frontend
        run: |
          npm ci
          npm run build

      - name: Set server version from tag
        run: |
          python3 - <<'PY'
          import os
          import re
          from pathlib import Path

          version = os.environ["RELEASE_VERSION"]
          path = Path("pyproject.toml")
          content = path.read_text(encoding="utf-8")
          updated, count = re.subn(
              r'(?m)^version = "[^"]+"$',
              f'version = "{version}"',
              content,
              count=1,
          )
          if count != 1:
              raise SystemExit("failed to update server version in pyproject.toml")
          path.write_text(updated, encoding="utf-8")
          PY

      - name: Show server build metadata
        run: |
          python3 - <<'PY'
          import tomllib
          with open("pyproject.toml", "rb") as f:
              data = tomllib.load(f)
          print(data["project"]["name"], data["project"]["version"])
          PY

      - name: Build peaky-peek-server wheel + sdist
        run: python -m build

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          skip-existing: true
        # Configure on PyPI: https://pypi.org/manage/account/publishing/
        #   Publisher: GitHub Actions
        #   Owner:     acailic
        #   Repo:      agent_debugger
        #   Workflow:  publish.yml
        #   Env name:  pypi-server
