name: Publish Python Packages to GitHub Packages

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to publish (e.g., 1.0.0)'
        required: true
        default: '1.0.0'

jobs:
  publish-agent-sdk:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Set up Python 3.13
      uses: actions/setup-python@v4
      with:
        python-version: '3.13'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install build twine

    - name: Build package
      run: |
        cd packages/py/aitbc-agent-sdk
        python -m build

    - name: Publish to GitHub Packages
      run: |
        cd packages/py/aitbc-agent-sdk
        python -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
      env:
        TWINE_USERNAME: __token__
        TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}

  publish-coordinator-api:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Set up Python 3.13
      uses: actions/setup-python@v4
      with:
        python-version: '3.13'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install build twine

    - name: Build package
      run: |
        cd apps/coordinator-api
        python -m build

    - name: Publish to GitHub Packages
      run: |
        cd apps/coordinator-api
        python -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
      env:
        TWINE_USERNAME: __token__
        TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}

  publish-blockchain-node:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Set up Python 3.13
      uses: actions/setup-python@v4
      with:
        python-version: '3.13'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install build twine

    - name: Build package
      run: |
        cd apps/blockchain-node
        python -m build

    - name: Publish to GitHub Packages
      run: |
        cd apps/blockchain-node
        python -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
      env:
        TWINE_USERNAME: __token__
        TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}

  publish-explorer-web:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'
        registry-url: 'https://npm.pkg.github.com'

    - name: Install dependencies
      run: |
        cd apps/explorer-web
        npm ci

    - name: Build package
      run: |
        cd apps/explorer-web
        npm run build

    - name: Publish to GitHub Packages
      run: |
        cd apps/explorer-web
        npm publish
      env:
        NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
