name: Industrial Launch (PyPI + Release)

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to release (e.g., v0.5.4)'
        required: true
        type: string

permissions:
  id-token: write
  contents: write

jobs:
  build-and-publish:
    name: Industrial Build & Publish
    runs-on: ubuntu-latest
    environment: release
    steps:
      - name: Checkout Source
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Setup uv
        uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true
          version: "latest"

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

      - name: Build SOTA Artifacts
        run: |
          uv build
          if [ -d "mcpb" ]; then
            zip -r dist/windows-computer-use-mcp.mcpb mcpb/ -x "*.git*"
          fi

      - name: Generate Changelog
        id: changelog
        run: |
          if git describe --tags --abbrev=0 HEAD^ >/dev/null 2>&1; then
            PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD^)
            CHANGELOG=$(git log --pretty=format:"- %s" $PREVIOUS_TAG..HEAD)
          else
            CHANGELOG=$(git log --pretty=format:"- %s" --reverse)
          fi
          echo "notes<<EOF" >> $GITHUB_OUTPUT
          echo "$CHANGELOG" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Publish to PyPI (OIDC)
        if: startsWith(github.ref, 'refs/tags/v')
        uses: pypa/gh-action-pypi-publish@release/v1

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          name: "windows-computer-use-mcp ${{ github.ref_name }}"
          body: |
            ## ${{ github.ref_name }}

            **Desktop:** download the NSIS `.exe` or MSI from CI / local `npm run tauri:build` (not attached by this workflow).

            **Claude Desktop:** `windows-computer-use-mcp.mcpb` below.

            **Python (PyPI):** `uvx windows-computer-use-mcp` — wheel is on PyPI, not duplicated here.

            ### What's New
            ${{ steps.changelog.outputs.notes }}
          files: |
            dist/*.mcpb
          draft: false
          prerelease: ${{ contains(github.ref, 'alpha') || contains(github.ref, 'beta') || contains(github.ref, 'rc') }}
