name: Release

on:
  push:
    tags:
      - "v*"

permissions:
  contents: write
  id-token: write

jobs:
  # ── Build Python package ────────────────────────────────────────
  build-python:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

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

      - name: Install build tools
        run: pip install build hatchling

      - name: Build package
        run: python -m build

      - name: Upload Python artifacts
        uses: actions/upload-artifact@v4
        with:
          name: dist-python
          path: dist/

  # ── Build VS Code extension ────────────────────────────────────
  build-extension:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: extension
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Install dependencies
        run: npm ci

      - name: Build extension
        run: npm run package

      - name: Package VSIX
        run: npx @vscode/vsce package --no-dependencies -o mcp-grid.vsix

      - name: Upload VSIX artifact
        uses: actions/upload-artifact@v4
        with:
          name: dist-vsix
          path: extension/mcp-grid.vsix

  # ── Publish to PyPI ─────────────────────────────────────────────
  publish-pypi:
    needs: build-python
    runs-on: ubuntu-latest
    environment:
      name: pypi
      url: https://pypi.org/project/mcp-grid/
    steps:
      - name: Download Python artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist-python
          path: dist/

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}

  # ── Publish to VS Code Marketplace ─────────────────────────────
  publish-vsce:
    needs: build-extension
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20

      - name: Download VSIX
        uses: actions/download-artifact@v4
        with:
          name: dist-vsix
          path: .

      - name: Publish to VS Code Marketplace
        run: npx @vscode/vsce publish --packagePath mcp-grid.vsix
        env:
          VSCE_PAT: ${{ secrets.VSCE_PAT }}

  # ── GitHub Release ──────────────────────────────────────────────
  github-release:
    needs: [build-python, build-extension]
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4

      - name: Download Python artifacts
        uses: actions/download-artifact@v4
        with:
          name: dist-python
          path: dist/

      - name: Download VSIX artifact
        uses: actions/download-artifact@v4
        with:
          name: dist-vsix
          path: .

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            dist/*
            mcp-grid.vsix
          generate_release_notes: true
          draft: false
          prerelease: ${{ contains(github.ref, 'rc') || contains(github.ref, 'beta') || contains(github.ref, 'alpha') }}
