#!/bin/bash
# Companion AI Installation Script for Linux

set -e

# Configuration
INSTALL_DIR="/opt/companion"
DATA_DIR="/var/lib/companion"
USER="companion"
SERVICE_USER="${USER}"
REPO_URL="https://github.com/santhoshjan/companion.git"

echo "Companion AI Installation Script"
echo "================================"
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then 
    echo "Please run as root (use sudo)"
    exit 1
fi

# Check dependencies
echo "Checking dependencies..."
if ! command -v python3 &> /dev/null; then
    echo "Python 3 is required but not installed. Aborting."
    exit 1
fi

if ! command -v pip3 &> /dev/null; then
    echo "pip3 is required but not installed. Installing..."
    apt-get update && apt-get install -y python3-pip
fi

# Create user
echo "Creating companion user..."
if ! id "$USER" &>/dev/null; then
    useradd -r -s /bin/false -d "$INSTALL_DIR" "$USER"
fi

# Create directories
echo "Creating directories..."
mkdir -p "$INSTALL_DIR"
mkdir -p "$DATA_DIR"/{vectors,memory,models}
mkdir -p /etc/companion

# Install application
echo "Installing Companion AI..."
if [ -d ".git" ]; then
    # Running from git repo
    cp -r . "$INSTALL_DIR/"
else
    # Clone from remote
    git clone "$REPO_URL" "$INSTALL_DIR"
fi

# Create virtual environment
echo "Creating Python virtual environment..."
cd "$INSTALL_DIR"
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -e ".[dev]"

# Set permissions
echo "Setting permissions..."
chown -R "$USER:$USER" "$INSTALL_DIR"
chown -R "$USER:$USER" "$DATA_DIR"

# Install systemd services
echo "Installing systemd services..."
if command -v systemctl &> /dev/null; then
    cp systemd/companion-api.service /etc/systemd/system/
    cp systemd/companion-indexer.service /etc/systemd/system/
    cp systemd/companion-index@.service /etc/systemd/system/
    cp systemd/companion-index.timer /etc/systemd/system/ 2>/dev/null || true
    
    systemctl daemon-reload
    
    echo ""
    echo "Services installed. To start:"
    echo "  sudo systemctl enable companion-api"
    echo "  sudo systemctl start companion-api"
    echo ""
    echo "For auto-indexing:"
    echo "  sudo systemctl enable companion-indexer"
    echo "  sudo systemctl start companion-indexer"
else
    echo "systemd not found. Manual setup required."
fi

# Create config if doesn't exist
if [ ! -f /etc/companion/config.json ]; then
    echo "Creating default configuration..."
    cp "$INSTALL_DIR/config.json" /etc/companion/config.json
    # Update paths in config
    sed -i "s|~/.companion|$DATA_DIR|g" /etc/companion/config.json
fi

# Create symlink for CLI
echo "Installing CLI..."
ln -sf "$INSTALL_DIR/venv/bin/companion" /usr/local/bin/companion 2>/dev/null || true

echo ""
echo "Installation complete!"
echo "======================"
echo ""
echo "Next steps:"
echo "1. Edit configuration: sudo nano /etc/companion/config.json"
echo "2. Update vault path in the config"
echo "3. Start services: sudo systemctl start companion-api companion-indexer"
echo "4. Check status: sudo systemctl status companion-api"
echo ""
echo "Data directory: $DATA_DIR"
echo "Config directory: /etc/companion"
echo "Install directory: $INSTALL_DIR"
