name: Beta Release (TestPyPI)

concurrency:
  group: beta-release
  cancel-in-progress: true

on:
  push:
    branches:
      - beta
    paths:
      - "Server/**"

jobs:
  publish_testpypi:
    name: Publish beta to TestPyPI
    runs-on: ubuntu-latest
    environment:
      name: testpypi
      url: https://test.pypi.org/p/mcpforunityserver
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Install uv
        uses: astral-sh/setup-uv@v7
        with:
          version: "latest"
          enable-cache: true
          cache-dependency-glob: "Server/uv.lock"

      - name: Generate dev version
        id: version
        shell: bash
        run: |
          set -euo pipefail
          BASE_VERSION=$(grep -oP '(?<=version = ")[^"]+' Server/pyproject.toml)
          # Use date for unique dev version (PEP 440 compliant: X.Y.Z.devN)
          # Note: PyPI/TestPyPI don't support local version identifiers (+...)
          DEV_NUMBER="$(date +%Y%m%d%H%M%S)"
          DEV_VERSION="${BASE_VERSION}.dev${DEV_NUMBER}"
          echo "Base version: $BASE_VERSION"
          echo "Dev version: $DEV_VERSION"
          echo "dev_version=$DEV_VERSION" >> "$GITHUB_OUTPUT"

      - name: Update version for beta release
        env:
          DEV_VERSION: ${{ steps.version.outputs.dev_version }}
        shell: bash
        run: |
          set -euo pipefail
          sed -i "s/^version = .*/version = \"${DEV_VERSION}\"/" Server/pyproject.toml
          echo "Updated pyproject.toml:"
          grep "^version" Server/pyproject.toml

      - name: Build a binary wheel and a source tarball
        shell: bash
        run: uv build
        working-directory: ./Server

      - name: Publish distribution to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          packages-dir: Server/dist/
          repository-url: https://test.pypi.org/legacy/
