#!/usr/bin/env bash
set -euo pipefail

# Apollo CLI Installer
# https://github.com/dipankar/apollo-io-cli

BINARY_NAME="apollo"
INSTALL_DIR="${INSTALL_DIR:-/usr/local/bin}"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1"
    exit 1
}

# Check if a command exists
command_exists() {
    command -v "$1" >/dev/null 2>&1
}

# Check for required dependencies
check_dependencies() {
    info "Checking dependencies..."

    if ! command_exists cargo; then
        error "Rust/Cargo is not installed. Please install Rust first:
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
    fi

    info "Found cargo: $(cargo --version)"
}

# Build the project
build_project() {
    info "Building Apollo CLI in release mode..."
    cargo build --release

    if [[ ! -f "target/release/${BINARY_NAME}" ]]; then
        error "Build failed: binary not found at target/release/${BINARY_NAME}"
    fi

    info "Build successful!"
}

# Install the binary
install_binary() {
    local target_path="${INSTALL_DIR}/${BINARY_NAME}"

    info "Installing ${BINARY_NAME} to ${INSTALL_DIR}..."

    # Create install directory if it doesn't exist
    if [[ ! -d "${INSTALL_DIR}" ]]; then
        if [[ "${INSTALL_DIR}" == /usr/local/bin ]]; then
            sudo mkdir -p "${INSTALL_DIR}"
        else
            mkdir -p "${INSTALL_DIR}"
        fi
    fi

    # Copy binary to install directory
    if [[ "${INSTALL_DIR}" == /usr/local/bin ]] || [[ ! -w "${INSTALL_DIR}" ]]; then
        sudo cp "target/release/${BINARY_NAME}" "${target_path}"
        sudo chmod +x "${target_path}"
    else
        cp "target/release/${BINARY_NAME}" "${target_path}"
        chmod +x "${target_path}"
    fi

    info "Installed ${BINARY_NAME} to ${target_path}"
}

# Verify installation
verify_installation() {
    if command_exists "${BINARY_NAME}"; then
        info "Installation verified! You can now use '${BINARY_NAME}'"
        echo ""
        "${BINARY_NAME}" --version 2>/dev/null || "${BINARY_NAME}" --help | head -5
    else
        warn "${BINARY_NAME} is installed but not in your PATH"
        warn "Add ${INSTALL_DIR} to your PATH:"
        echo ""
        echo "    export PATH=\"${INSTALL_DIR}:\$PATH\""
        echo ""
        echo "Add this to your ~/.bashrc or ~/.zshrc to make it permanent."
    fi
}

# Print usage instructions
print_usage() {
    echo ""
    info "Quick Start:"
    echo "  1. Set your API key:"
    echo "     export APOLLO_API_KEY=\"your_api_key_here\""
    echo ""
    echo "  2. Try a command:"
    echo "     ${BINARY_NAME} --help"
    echo "     ${BINARY_NAME} operations"
    echo ""
}

# Main installation flow
main() {
    echo ""
    echo "======================================"
    echo "   Apollo CLI Installer"
    echo "======================================"
    echo ""

    # Allow custom install directory
    if [[ -n "${1:-}" ]]; then
        INSTALL_DIR="$1"
        info "Using custom install directory: ${INSTALL_DIR}"
    fi

    check_dependencies
    build_project
    install_binary
    verify_installation
    print_usage

    echo ""
    info "Installation complete!"
}

main "$@"
