name: Release

on:
  push:
    tags:
      - 'v*'
  workflow_dispatch:
    inputs:
      tag:
        description: 'Version tag (e.g., v2.0.1)'
        required: true

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact: mcp-gateway-linux-x86_64
            packages: musl-tools
          - os: ubuntu-24.04-arm
            target: aarch64-unknown-linux-musl
            artifact: mcp-gateway-linux-aarch64
            packages: musl-tools gcc-aarch64-linux-gnu
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact: mcp-gateway-darwin-arm64
            packages: ""
          - os: macos-15
            target: x86_64-apple-darwin
            artifact: mcp-gateway-darwin-x86_64
            packages: ""

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4

      - name: Install system packages
        if: matrix.packages != ''
        run: sudo apt-get update && sudo apt-get install -y ${{ matrix.packages }}

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

      - name: Build
        run: cargo build --release --target ${{ matrix.target }}

      - name: Rename binary
        run: |
          cp target/${{ matrix.target }}/release/mcp-gateway ${{ matrix.artifact }}
          chmod +x ${{ matrix.artifact }}

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

  release:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v4

      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Prepare release files
        run: |
          mkdir -p release
          find artifacts -type f -name 'mcp-gateway-*' -exec cp {} release/ \;
          ls -la release/

      - name: Create checksums
        run: |
          cd release
          sha256sum * > SHA256SUMS.txt
          cat SHA256SUMS.txt

      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: ${{ github.event.inputs.tag || github.ref_name }}
          name: MCP Gateway ${{ github.event.inputs.tag || github.ref_name }}
          body: |
            ## Installation

            ### Cargo (Recommended)
            ```bash
            cargo install mcp-gateway
            ```

            ### Manual Download
            Download the appropriate binary for your platform from the assets below.

            **Supported platforms:**
            - `mcp-gateway-darwin-arm64` - macOS Apple Silicon (M1/M2/M3)
            - `mcp-gateway-darwin-x86_64` - macOS Intel
            - `mcp-gateway-linux-x86_64` - Linux x86_64 (static musl)
            - `mcp-gateway-linux-aarch64` - Linux ARM64 (static musl)

            ## Verify checksums
            ```bash
            sha256sum -c SHA256SUMS.txt
            ```

            ## Quick Start
            ```bash
            # Create config file
            cat > servers.yaml << 'EOF'
            server:
              port: 39400
            meta_mcp:
              enabled: true
            backends: {}
            EOF

            # Start gateway
            mcp-gateway --config servers.yaml
            ```

            See [README](https://github.com/MikkoParkkola/mcp-gateway#readme) for full documentation.
          files: |
            release/*
          draft: false
          prerelease: false

  publish:
    needs: release
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Publish to crates.io
        run: cargo publish --no-verify --allow-dirty
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
