name: Publish Release

on:
  push:
    tags:
      - 'v*'

jobs:
  publish-npm:
    name: Publish to npm
    runs-on: ubuntu-latest
    timeout-minutes: 30  # 添加超时保护
    permissions:
      contents: read
    outputs:
      version: ${{ steps.version.outputs.version }}
      is_prerelease: ${{ steps.check_prerelease.outputs.is_prerelease }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'

      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 10

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - name: Configure git for HTTPS
        run: |
          git config --global url."https://github.com/".insteadOf "git@github.com:"
          git config --global url."https://".insteadOf "ssh://"

      - name: Extract version and check prerelease
        id: version
        run: |
          VERSION=$(echo "${{ github.ref_name }}" | sed 's/^v//')
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "📦 Version: $VERSION"

      - name: Check if prerelease
        id: check_prerelease
        run: |
          if [[ "${{ github.ref_name }}" == *"beta"* ]] || [[ "${{ github.ref_name }}" == *"alpha"* ]] || [[ "${{ github.ref_name }}" == *"rc"* ]]; then
            echo "is_prerelease=true" >> $GITHUB_OUTPUT
          else
            echo "is_prerelease=false" >> $GITHUB_OUTPUT
          fi

      - name: Install dependencies
        run: pnpm install --ignore-scripts

      - name: Update version for prerelease
        if: contains(github.ref_name, 'beta') || contains(github.ref_name, 'alpha') || contains(github.ref_name, 'rc')
        run: |
          # For tags like v1.10.1-beta, auto-increment NPM version
          TAG_VERSION="${{ github.ref_name }}"
          TAG_VERSION="${TAG_VERSION#v}"  # Remove 'v' prefix
          
          if [[ "$TAG_VERSION" == *-beta ]]; then
            BASE_VERSION="${TAG_VERSION%-beta}"
            
            # Get the latest published beta version from npm
            LATEST_BETA=$(npm view @promptx/core versions --json | jq -r '.[]' | grep "^${BASE_VERSION}-beta\." | sort -V | tail -1 | sed 's/.*-beta\.//')
            
            if [[ -z "$LATEST_BETA" ]]; then
              # No existing beta versions, start from 0
              NEXT_VERSION="${BASE_VERSION}-beta.0"
            else
              # Increment the beta version
              NEXT_BETA=$((LATEST_BETA + 1))
              NEXT_VERSION="${BASE_VERSION}-beta.${NEXT_BETA}"
            fi
            
          else
            # For other prerelease types, use tag version as-is
            NEXT_VERSION="$TAG_VERSION"
          fi
          
          echo "Auto-incremented version: $NEXT_VERSION"
          echo "PACKAGE_VERSION=$NEXT_VERSION" >> $GITHUB_ENV
          
          # Update all package.json files
          for pkg in apps/cli apps/desktop packages/core packages/logger packages/mcp-server packages/resource; do
            jq --arg ver "$NEXT_VERSION" '.version = $ver' $pkg/package.json > tmp.json && mv tmp.json $pkg/package.json
            echo "Updated $pkg to version $NEXT_VERSION"
          done

      - name: Build packages
        run: pnpm build

      - name: Run tests
        run: pnpm test || true  # Temporarily skip test failures

      - name: Publish to npm
        run: |
          # Determine npm tag based on version
          if [[ "${{ github.ref_name }}" == *"beta"* ]]; then
            NPM_TAG="beta"
          elif [[ "${{ github.ref_name }}" == *"alpha"* ]]; then
            NPM_TAG="alpha"
          elif [[ "${{ github.ref_name }}" == *"rc"* ]]; then
            NPM_TAG="rc"
          else
            NPM_TAG="latest"
          fi
          
          echo "Publishing with tag: $NPM_TAG"
          
          # Publish workspace packages with appropriate tag
          pnpm -r publish --access public --no-git-checks --tag $NPM_TAG --registry https://registry.npmjs.org
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v1
        with:
          name: Release ${{ github.ref_name }}
          generate_release_notes: true
          draft: false
          prerelease: ${{ contains(github.ref_name, 'beta') || contains(github.ref_name, 'alpha') || contains(github.ref_name, 'rc') }}
        env:
          GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}

  # 拆分为并行的 Docker 构建 jobs，减少总时间
  publish-docker-amd64:
    name: Build Docker AMD64
    runs-on: ubuntu-latest
    timeout-minutes: 60  # AMD64 构建 60 分钟超时
    needs: publish-npm
    if: needs.publish-npm.outputs.is_prerelease == 'false'

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

      - name: Wait for npm package availability
        run: |
          VERSION="${{ needs.publish-npm.outputs.version }}"
          echo "Waiting for @promptx/mcp-server@$VERSION to be available on npm..."

          for i in {1..20}; do
            if npm view @promptx/mcp-server@$VERSION version 2>/dev/null; then
              echo "✅ Package is available"
              exit 0
            fi
            echo "Attempt $i/20: waiting 30s..."
            sleep 30
          done
          echo "❌ Timeout"
          exit 1

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_TOKEN }}

      - name: Build and push AMD64
        uses: docker/build-push-action@v5
        with:
          context: ./docker
          platforms: linux/amd64
          push: true
          tags: |
            deepracticexs/promptx:${{ needs.publish-npm.outputs.version }}-amd64
            deepracticexs/promptx:latest-amd64
          build-args: |
            VERSION=${{ needs.publish-npm.outputs.version }}
          cache-from: type=registry,ref=deepracticexs/promptx:buildcache-amd64
          cache-to: type=registry,ref=deepracticexs/promptx:buildcache-amd64,mode=max

  publish-docker-arm64:
    name: Build Docker ARM64
    runs-on: ubuntu-latest
    timeout-minutes: 180  # ARM64 构建慢，给 3 小时
    needs: publish-npm
    if: needs.publish-npm.outputs.is_prerelease == 'false'

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

      - name: Wait for npm package availability
        run: |
          VERSION="${{ needs.publish-npm.outputs.version }}"
          echo "Waiting for @promptx/mcp-server@$VERSION to be available on npm..."

          for i in {1..20}; do
            if npm view @promptx/mcp-server@$VERSION version 2>/dev/null; then
              echo "✅ Package is available"
              exit 0
            fi
            echo "Attempt $i/20: waiting 30s..."
            sleep 30
          done
          echo "❌ Timeout"
          exit 1

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v3

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_TOKEN }}

      - name: Build and push ARM64
        uses: docker/build-push-action@v5
        with:
          context: ./docker
          platforms: linux/arm64
          push: true
          tags: |
            deepracticexs/promptx:${{ needs.publish-npm.outputs.version }}-arm64
            deepracticexs/promptx:latest-arm64
          build-args: |
            VERSION=${{ needs.publish-npm.outputs.version }}
          cache-from: type=registry,ref=deepracticexs/promptx:buildcache-arm64
          cache-to: type=registry,ref=deepracticexs/promptx:buildcache-arm64,mode=max

  publish-docker-manifest:
    name: Create Docker Manifest
    runs-on: ubuntu-latest
    timeout-minutes: 10
    needs: [publish-npm, publish-docker-amd64, publish-docker-arm64]

    steps:
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_TOKEN }}

      - name: Create multi-platform manifest
        run: |
          VERSION="${{ needs.publish-npm.outputs.version }}"

          # 创建版本 manifest
          docker buildx imagetools create -t deepracticexs/promptx:${VERSION} \
            deepracticexs/promptx:${VERSION}-amd64 \
            deepracticexs/promptx:${VERSION}-arm64

          # 创建 latest manifest
          docker buildx imagetools create -t deepracticexs/promptx:latest \
            deepracticexs/promptx:latest-amd64 \
            deepracticexs/promptx:latest-arm64

          echo "✅ Multi-platform manifest created"

      - name: Summary
        run: |
          echo "## Docker Image Published 🎉" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "- **Version**: ${{ needs.publish-npm.outputs.version }}" >> $GITHUB_STEP_SUMMARY
          echo "- **Tags**:" >> $GITHUB_STEP_SUMMARY
          echo "  - \`deepracticexs/promptx:${{ needs.publish-npm.outputs.version }}\`" >> $GITHUB_STEP_SUMMARY
          echo "  - \`deepracticexs/promptx:latest\`" >> $GITHUB_STEP_SUMMARY
          echo "- **Platforms**: linux/amd64, linux/arm64" >> $GITHUB_STEP_SUMMARY
          echo "" >> $GITHUB_STEP_SUMMARY
          echo "### Usage" >> $GITHUB_STEP_SUMMARY
          echo "\`\`\`bash" >> $GITHUB_STEP_SUMMARY
          echo "docker run -d -p 5203:5203 -v ~/.promptx:/root/.promptx deepracticexs/promptx:${{ needs.publish-npm.outputs.version }}" >> $GITHUB_STEP_SUMMARY
          echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
