name: Publish SDKs

on:
  release:
    types: [published]
  workflow_dispatch:  # Manual trigger for testing

jobs:
  # ============================================
  # Python SDK → PyPI
  # ============================================
  publish-python:
    name: Publish Python SDK
    runs-on: ubuntu-latest
    # Only run if PYPI_API_TOKEN exists
    if: ${{ vars.PYPI_ENABLED == 'true' || github.event_name == 'workflow_dispatch' }}
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      
      - name: Install build tools
        run: pip install build twine
      
      - name: Build package
        run: |
          cd sdk/python
          python -m build
      
      - name: Publish to PyPI
        env:
          TWINE_USERNAME: __token__
          TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
        run: |
          cd sdk/python
          twine upload dist/* --skip-existing
        continue-on-error: true  # Don't fail if PyPI token not set yet

  # ============================================
  # TypeScript SDK → npm
  # ============================================
  publish-typescript:
    name: Publish TypeScript SDK
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'
      
      - name: Install dependencies
        run: |
          cd sdk-ts
          npm ci
      
      - name: Build
        run: |
          cd sdk-ts
          npm run build
      
      - name: Publish to npm
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          cd sdk-ts
          npm publish --access public
        continue-on-error: true  # Don't fail on duplicate version

  # ============================================
  # Rust SDK → crates.io
  # ============================================
  publish-rust:
    name: Publish Rust SDK
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable
      
      - name: Publish to crates.io
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          cd sdk-rust
          cargo publish --allow-dirty
        continue-on-error: true  # Don't fail on duplicate version

  # ============================================
  # Go SDK → Just needs tag (no publish step)
  # ============================================
  tag-go:
    name: Tag Go SDK
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Verify Go module
        run: |
          cd sdk-go
          go mod verify || true
      
      - name: Print install command
        run: |
          echo "✅ Go SDK available at:"
          echo "go get github.com/QWED-AI/qwed-verification/sdk-go@${{ github.ref_name }}"

  # ============================================
  # Summary
  # ============================================
  summary:
    name: Publish Summary
    needs: [publish-typescript, publish-rust, tag-go]
    runs-on: ubuntu-latest
    if: always()
    steps:
      - name: Summary
        run: |
          echo "=== SDK Publish Results ==="
          echo "TypeScript (npm): ${{ needs.publish-typescript.result }}"
          echo "Rust (crates.io): ${{ needs.publish-rust.result }}"
          echo "Go (tagged):      ${{ needs.tag-go.result }}"
          echo ""
          echo "Note: Python publish skipped until PyPI token is set"
