name: Simple Release

on:
  workflow_dispatch:
    inputs:
      version_bump:
        description: 'Version bump type'
        required: true
        type: choice
        options:
          - patch
          - minor
          - major

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1
  RUST_MIN_STACK: 8388608

jobs:
  bump-version:
    runs-on: ubuntu-22.04
    outputs:
      version: ${{ steps.version.outputs.version }}
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
      
      - name: Bump version
        id: version
        run: |
          # Get current version
          CURRENT=$(grep -m1 '^version = ' Cargo.toml | cut -d'"' -f2)
          echo "Current version: $CURRENT"
          
          # Parse version components
          IFS='.' read -ra PARTS <<< "$CURRENT"
          MAJOR="${PARTS[0]}"
          MINOR="${PARTS[1]}"
          PATCH="${PARTS[2]}"
          
          # Bump based on input
          case "${{ inputs.version_bump }}" in
            major)
              MAJOR=$((MAJOR + 1))
              MINOR=0
              PATCH=0
              ;;
            minor)
              MINOR=$((MINOR + 1))
              PATCH=0
              ;;
            patch)
              PATCH=$((PATCH + 1))
              ;;
          esac
          
          NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}"
          echo "New version: $NEW_VERSION"
          echo "version=$NEW_VERSION" >> "$GITHUB_OUTPUT"
          
          # Update all Cargo.toml files
          sed -i "0,/^version = /s/^version = .*/version = \"$NEW_VERSION\"/" Cargo.toml
          sed -i "0,/^version = /s/^version = .*/version = \"$NEW_VERSION\"/" Cargo.toml
      
      - name: Commit and tag
        run: |
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
          
          # Check if tag already exists
          if git ls-remote --tags origin | grep -q "refs/tags/v${{ steps.version.outputs.version }}$"; then
            echo "Tag v${{ steps.version.outputs.version }} already exists, skipping..."
            exit 0
          fi
          
          git add -A
          git commit -m "chore: release v${{ steps.version.outputs.version }}"
          git pull --rebase origin master
          git tag "v${{ steps.version.outputs.version }}"
          git push origin master
          git push origin "v${{ steps.version.outputs.version }}"

  build-binaries:
    needs: bump-version
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-22.04
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v4
        with:
          ref: v${{ needs.bump-version.outputs.version }}
      
      - uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}
      
      - name: Install Linux dependencies
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install -y libssl-dev mold clang
          if [ "${{ matrix.target }}" = "aarch64-unknown-linux-gnu" ]; then
            sudo apt-get install -y gcc-aarch64-linux-gnu
          fi
      
      
      - name: Build binary
        run: |
          cd server
          cargo build --release --target ${{ matrix.target }}
          cd ..
          
      - name: Create archive
        run: |
          cd target/${{ matrix.target }}/release
          tar -czf ../../../paiml-mcp-agent-toolkit-${{ matrix.target }}.tar.gz pmat
          cd ../../..
          
      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: binary-${{ matrix.target }}
          path: paiml-mcp-agent-toolkit-${{ matrix.target }}.tar.gz

  create-release:
    needs: [bump-version, build-binaries]
    runs-on: ubuntu-22.04
    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 release-files
          find artifacts -name "*.tar.gz" -exec cp {} release-files/ \;
          ls -la release-files/
      
      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ needs.bump-version.outputs.version }}
          name: v${{ needs.bump-version.outputs.version }}
          files: release-files/*
          generate_release_notes: true