name: "Setup Go"
description: "Setup Go environment by downloading and extracting from go.dev"
inputs:
  go-version:
    description: "The Go version to download and use"
    required: true
runs:
  using: "composite"
  steps:
    - name: Download and setup Go
      shell: bash
      run: |
        set -e
        GO_VERSION="${{ inputs.go-version }}"
        GO_VERSION="${GO_VERSION#go}" # Remove 'go' prefix if present

        # Determine OS and architecture
        if [[ "$RUNNER_OS" == "macOS" ]]; then
          OS="darwin"
          ARCH="arm64"
        else
          OS="linux"
          ARCH="amd64"
        fi

        DOWNLOAD_URL="https://go.dev/dl/go${GO_VERSION}.${OS}-${ARCH}.tar.gz"
        echo "Downloading Go from: ${DOWNLOAD_URL}"

        # Create temporary directory for download
        TMP_DIR=$(mktemp -d)
        curl -L "${DOWNLOAD_URL}" -o "${TMP_DIR}/go.tar.gz"

        # Remove existing Go installation if any
        sudo rm -rf /usr/local/go

        # Extract to /usr/local
        sudo tar -C /usr/local -xzf "${TMP_DIR}/go.tar.gz"

        # Clean up
        rm -rf "${TMP_DIR}"

        # Add to PATH
        echo "/usr/local/go/bin" >> $GITHUB_PATH
        echo "$HOME/go/bin" >> $GITHUB_PATH

    - name: Verify Go installation
      shell: bash
      run: |
        # Verify installation
        echo "Verifying Go installation..."
        go version
