#!/bin/bash
# Otto by Platoona - macOS Agent Installer
# https://platoona.com/otto

set -e

INSTALL_DIR="$HOME/.otto"
REPO_URL="https://github.com/Platoona/otto.git"
TMP_DIR=""

# Cleanup function
cleanup() {
    if [ -n "$TMP_DIR" ] && [ -d "$TMP_DIR" ]; then
        rm -rf "$TMP_DIR"
    fi
}
trap cleanup EXIT

echo ""
echo "  ___  _   _        "
echo " / _ \\| |_| |_ ___  "
echo "| | | | __| __/ _ \\ "
echo "| |_| | |_| || (_) |"
echo " \\___/ \\__|\\__\\___/ "
echo ""
echo "Otto by Platoona - macOS Agent Installer"
echo "========================================="
echo ""

# Check for git
if ! command -v git &> /dev/null; then
    echo "Error: git is not installed."
    exit 1
fi

# Check for Swift
if ! command -v swift &> /dev/null; then
    echo "Error: Swift is not installed."
    echo "Please install Xcode or Xcode Command Line Tools:"
    echo "  xcode-select --install"
    exit 1
fi

# Check for Python 3
if ! command -v python3 &> /dev/null; then
    echo "Error: Python 3 is not installed."
    echo "Please install Python 3 from https://python.org or via Homebrew:"
    echo "  brew install python3"
    exit 1
fi

# Determine source directory
# If Package.swift exists in current or parent dirs, use that (local install)
# Otherwise, clone the repo (remote install via curl)
SCRIPT_DIR="$(cd "$(dirname "$0" 2>/dev/null)" 2>/dev/null && pwd 2>/dev/null)" || SCRIPT_DIR=""
SOURCE_DIR=""

if [ -n "$SCRIPT_DIR" ] && [ -f "$SCRIPT_DIR/Package.swift" ]; then
    # Running from cloned repo
    SOURCE_DIR="$SCRIPT_DIR"
    echo "Installing from local directory..."
else
    # Running via curl | bash - need to clone
    echo "Downloading Otto..."
    TMP_DIR=$(mktemp -d)
    git clone --depth 1 --quiet "$REPO_URL" "$TMP_DIR"
    SOURCE_DIR="$TMP_DIR/macos"
fi

# Build the Swift binary
echo "Building Otto (this may take a minute)..."
cd "$SOURCE_DIR"
swift build -c release 2>&1 | grep -v "warning:" || true

# Create install directory
echo "Installing to $INSTALL_DIR..."
mkdir -p "$INSTALL_DIR"

# Copy binary
cp .build/release/otto "$INSTALL_DIR/otto"

# Copy Python agent and requirements
cp agent.py "$INSTALL_DIR/"
cp requirements.txt "$INSTALL_DIR/"

# Install Python dependencies
echo "Installing Python dependencies..."
python3 -m pip install openai || pip install openai || {
    echo ""
    echo "Failed to install openai. Please run manually:"
    echo "  pip install openai"
}

# Add to PATH
add_to_path() {
    local shell_rc="$1"
    local path_line='export PATH="$HOME/.otto:$PATH"'

    if [ -f "$shell_rc" ]; then
        if ! grep -q '\.otto' "$shell_rc"; then
            echo "" >> "$shell_rc"
            echo "# Otto by Platoona" >> "$shell_rc"
            echo "$path_line" >> "$shell_rc"
            echo "Added to $shell_rc"
        fi
    fi
}

# Detect shell and add to appropriate config
if [ -n "$ZSH_VERSION" ] || [ -f "$HOME/.zshrc" ]; then
    add_to_path "$HOME/.zshrc"
fi

if [ -f "$HOME/.bashrc" ]; then
    add_to_path "$HOME/.bashrc"
fi

if [ -f "$HOME/.bash_profile" ]; then
    add_to_path "$HOME/.bash_profile"
fi

echo ""
echo "Installation complete!"
echo ""

# Add to current session PATH
export PATH="$HOME/.otto:$PATH"

# Run setup automatically
echo "Starting Otto setup..."
echo ""
"$INSTALL_DIR/otto" setup

echo ""
echo "To use 'otto' command, restart your terminal or run:"
echo "  source ~/.zshrc"
echo ""
echo "For help: otto --help"
echo ""
