# .github/workflows/release-tester.yml
name: Release Tester Binary

on:
  release:
    types: [published]
  workflow_dispatch:

jobs:
  build-release:
    name: Build and Release Tester
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            asset_name: mcp-tester-linux-x86_64
            target: x86_64-unknown-linux-gnu
          - os: macos-latest
            asset_name: mcp-tester-macos-x86_64
            target: x86_64-apple-darwin
          # Note: Apple Silicon cross-compilation disabled due to ring crate limitations
          # GitHub Actions doesn't provide ARM64 macOS runners yet
          # - os: macos-latest
          #   asset_name: mcp-tester-macos-aarch64
          #   target: aarch64-apple-darwin
          - os: windows-latest
            asset_name: mcp-tester-windows-x86_64.exe
            target: x86_64-pc-windows-msvc

    steps:
      - name: Checkout repository
        uses: actions/checkout@v6

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

      - name: Build binary
        run: cargo build --release --package mcp-tester --target ${{ matrix.target }}

      - name: Prepare artifact for upload
        shell: bash
        run: |
          cd target/${{ matrix.target }}/release
          if [ "${{ matrix.os }}" = "windows-latest" ]; then
            mv mcp-tester.exe ${{ matrix.asset_name }}
            # Use relative path from repo root for Windows
            echo "ASSET_PATH=target/${{ matrix.target }}/release/${{ matrix.asset_name }}" >> $GITHUB_ENV
          else
            mv mcp-tester ${{ matrix.asset_name }}
            echo "ASSET_PATH=target/${{ matrix.target }}/release/${{ matrix.asset_name }}" >> $GITHUB_ENV
          fi

      - name: Get release tag
        id: get_tag
        shell: bash
        run: |
          if [ "${{ github.event_name }}" = "release" ]; then
            echo "TAG_NAME=${{ github.event.release.tag_name }}" >> $GITHUB_ENV
          else
            # For workflow_dispatch or push, use the latest release tag or create a dev release
            TAG=$(gh release list --limit 1 --json tagName -q '.[0].tagName' || echo "")
            if [ -z "$TAG" ]; then
              # No releases exist, create a dev release
              TAG="dev-$(date +%Y%m%d-%H%M%S)"
              gh release create "$TAG" --title "Development Build $TAG" --notes "Automated development build" --prerelease
            fi
            echo "TAG_NAME=${TAG}" >> $GITHUB_ENV
          fi
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload release asset
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release upload "${{ env.TAG_NAME }}" "${{ env.ASSET_PATH }}" --clobber