name: Build and Publish npm

on:
  push:
    tags:
      - 'v*'

jobs:
  build:
    name: Build ${{ matrix.platform }}
    runs-on: ${{ matrix.runner }}
    strategy:
      matrix:
        include:
          - platform: darwin-arm64
            target: aarch64-apple-darwin
            runner: macos-latest
          - platform: darwin-x64
            target: x86_64-apple-darwin
            runner: macos-latest
          - platform: linux-arm64
            target: aarch64-unknown-linux-musl
            runner: ubuntu-latest
          - platform: linux-x64
            target: x86_64-unknown-linux-musl
            runner: ubuntu-latest
          - platform: win32-x64
            target: x86_64-pc-windows-msvc
            runner: windows-latest
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.platform }}
          cache-on-failure: true

      - name: Install cross-compilation tools (Linux ARM64)
        if: matrix.platform == 'linux-arm64'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu musl-tools
          pip3 install cargo-zigbuild
          sudo snap install zig --classic --beta

      - name: Install musl tools (Linux x64)
        if: matrix.platform == 'linux-x64'
        run: sudo apt-get update && sudo apt-get install -y musl-tools perl make

      - name: Install protobuf compiler (Unix)
        if: runner.os != 'Windows'
        run: |
          if [ "$RUNNER_OS" = "macOS" ]; then
            brew install protobuf
          else
            sudo apt-get install -y protobuf-compiler
          fi

      - name: Install protobuf compiler (Windows)
        if: runner.os == 'Windows'
        run: choco install protoc -y

      - name: Build (Linux ARM64)
        if: matrix.platform == 'linux-arm64'
        run: cargo zigbuild --release --target ${{ matrix.target }} --features vendored-openssl

      - name: Build (Linux x64)
        if: matrix.platform == 'linux-x64'
        run: cargo build --release --target ${{ matrix.target }} --features vendored-openssl

      - name: Build (other)
        if: matrix.platform != 'linux-arm64' && matrix.platform != 'linux-x64'
        run: cargo build --release --target ${{ matrix.target }}

      - name: Prepare binary (Unix)
        if: runner.os != 'Windows'
        run: |
          mkdir -p staging
          cp target/${{ matrix.target }}/release/mentedb-mcp staging/
          chmod +x staging/mentedb-mcp

      - name: Prepare binary (Windows)
        if: runner.os == 'Windows'
        run: |
          mkdir staging
          copy target\${{ matrix.target }}\release\mentedb-mcp.exe staging\

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: binary-${{ matrix.platform }}
          path: staging/

  publish-npm:
    name: Publish to npm
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: lts/*
          registry-url: https://registry.npmjs.org

      - name: Sync versions from Cargo.toml
        run: node scripts/sync-npm-version.js

      - name: Download darwin-arm64
        uses: actions/download-artifact@v4
        with:
          name: binary-darwin-arm64
          path: npm/darwin-arm64/bin/

      - name: Download darwin-x64
        uses: actions/download-artifact@v4
        with:
          name: binary-darwin-x64
          path: npm/darwin-x64/bin/

      - name: Download linux-arm64
        uses: actions/download-artifact@v4
        with:
          name: binary-linux-arm64
          path: npm/linux-arm64/bin/

      - name: Download linux-x64
        uses: actions/download-artifact@v4
        with:
          name: binary-linux-x64
          path: npm/linux-x64/bin/

      - name: Download win32-x64
        uses: actions/download-artifact@v4
        with:
          name: binary-win32-x64
          path: npm/win32-x64/bin/

      - name: Set executable permissions
        run: |
          chmod +x npm/darwin-arm64/bin/mentedb-mcp
          chmod +x npm/darwin-x64/bin/mentedb-mcp
          chmod +x npm/linux-arm64/bin/mentedb-mcp
          chmod +x npm/linux-x64/bin/mentedb-mcp
          chmod +x npm/mentedb-mcp/bin/mentedb-mcp

      - name: Copy README to npm package
        run: cp README.md npm/mentedb-mcp/README.md

      - name: Publish platform packages
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          for pkg in darwin-arm64 darwin-x64 linux-arm64 linux-x64 win32-x64; do
            cd npm/$pkg
            npm publish --access public || true
            cd ../..
          done

      - name: Publish main package
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          cd npm/mentedb-mcp
          npm publish --access public || echo "Already published, skipping"
