#!/bin/bash

# Stock Bot - Host Machine Dependencies Setup Script
# This script installs all necessary system dependencies for running the stock bot
# with browser automation capabilities

set -e  # Exit on any error

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

# Helper functions
log_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

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

# Check if running as root
check_not_root() {
    if [ "$EUID" -eq 0 ]; then
        log_error "Please do not run this script as root. It will use sudo when necessary."
        exit 1
    fi
}

# Detect OS
detect_os() {
    if [[ -f /etc/os-release ]]; then
        . /etc/os-release
        OS=$ID
        VERSION=$VERSION_ID
        log_info "Detected OS: $OS $VERSION"
    else
        log_error "Cannot detect OS. This script supports Ubuntu/Debian systems."
        exit 1
    fi
}

# Update package lists
update_packages() {
    log_info "Updating package lists..."
    sudo apt update
    log_success "Package lists updated"
}

# Install system dependencies for Playwright
install_playwright_deps() {
    log_info "Installing Playwright system dependencies..."
    
    # Install basic dependencies
    sudo apt install -y \
        wget \
        curl \
        gnupg \
        ca-certificates \
        software-properties-common \
        apt-transport-https
    
    # Install browser dependencies
    sudo apt install -y \
        libnss3 \
        libnspr4 \
        libatk-bridge2.0-0 \
        libdrm2 \
        libxkbcommon0 \
        libxcomposite1 \
        libxdamage1 \
        libxrandr2 \
        libgbm1 \
        libxss1 \
        libasound2 \
        libatspi2.0-0 \
        libgtk-3-0
    
    # Install additional media and graphics libraries
    sudo apt install -y \
        libgconf-2-4 \
        libxfixes3 \
        libxinerama1 \
        libxi6 \
        libxrandr2 \
        libasound2-dev \
        libpangocairo-1.0-0 \
        libcairo-gobject2 \
        libcairo2 \
        libgdk-pixbuf2.0-0 \
        libgtk-3-0 \
        libglib2.0-0 \
        libpango-1.0-0 \
        libharfbuzz0b \
        libfreetype6 \
        libfontconfig1
    
    # Install fonts
    sudo apt install -y \
        fonts-liberation \
        fonts-noto-color-emoji \
        fonts-noto-cjk
    
    log_success "Playwright system dependencies installed"
}

# Install development tools
install_dev_tools() {
    log_info "Installing development tools..."
    
    sudo apt install -y \
        build-essential \
        git \
        curl \
        wget \
        unzip \
        vim \
        htop \
        jq \
        tree
    
    log_success "Development tools installed"
}

# Install Docker (if not already installed)
install_docker() {
    if command -v docker &> /dev/null; then
        log_info "Docker is already installed"
        return
    fi
    
    log_info "Installing Docker..."
    
    # Add Docker's official GPG key
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    
    # Add Docker repository
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    
    # Update package lists and install Docker
    sudo apt update
    sudo apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
    
    # Add user to docker group
    sudo usermod -aG docker $USER
    
    log_success "Docker installed. Please log out and back in for group changes to take effect."
}

# Install Node.js and Bun (if not already installed)
install_runtime() {
    # Check if Bun is installed
    if command -v bun &> /dev/null; then
        log_info "Bun is already installed: $(bun --version)"
    else
        log_info "Installing Bun..."
        curl -fsSL https://bun.sh/install | bash
        export PATH="$HOME/.bun/bin:$PATH"
        log_success "Bun installed"
    fi
    
    # Check if Node.js is installed
    if command -v node &> /dev/null; then
        log_info "Node.js is already installed: $(node --version)"
    else
        log_info "Installing Node.js..."
        curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
        sudo apt install -y nodejs
        log_success "Node.js installed"
    fi
}

# Install additional system dependencies for proxy and networking
install_network_deps() {
    log_info "Installing networking and proxy dependencies..."
    
    sudo apt install -y \
        net-tools \
        iputils-ping \
        telnet \
        netcat \
        socat \
        proxychains4 \
        tor \
        privoxy
    
    log_success "Networking dependencies installed"
}

# Install system monitoring tools
install_monitoring_tools() {
    log_info "Installing system monitoring tools..."
    
    sudo apt install -y \
        htop \
        iotop \
        nethogs \
        iftop \
        dstat \
        sysstat \
        lsof
    
    log_success "Monitoring tools installed"
}

# Configure system limits for better performance
configure_system_limits() {
    log_info "Configuring system limits..."
    
    # Increase file descriptor limits
    echo "fs.file-max = 2097152" | sudo tee -a /etc/sysctl.conf
    echo "* soft nofile 65536" | sudo tee -a /etc/security/limits.conf
    echo "* hard nofile 65536" | sudo tee -a /etc/security/limits.conf
    
    # Apply sysctl changes
    sudo sysctl -p
    
    log_success "System limits configured"
}

# Install Playwright browsers
install_playwright_browsers() {
    log_info "Installing Playwright browsers..."
    
    # Navigate to project directory
    cd "$(dirname "$0")/.."
    
    # Install Playwright browsers
    bunx playwright install chromium
    bunx playwright install firefox
    bunx playwright install webkit
    
    log_success "Playwright browsers installed"
}

# Main installation function
main() {
    log_info "Starting Stock Bot host dependencies setup..."
    
    check_not_root
    detect_os
    
    # Only proceed if Ubuntu/Debian
    if [[ "$OS" != "ubuntu" && "$OS" != "debian" ]]; then
        log_error "This script only supports Ubuntu and Debian systems"
        exit 1
    fi
    
    # Run installation steps
    update_packages
    install_dev_tools
    install_playwright_deps
    install_runtime
    install_docker
    install_network_deps
    install_monitoring_tools
    configure_system_limits
    install_playwright_browsers
    
    log_success "Host dependencies setup completed!"
    log_info "Next steps:"
    echo "  1. Log out and back in (or run 'newgrp docker') to activate Docker group membership"
    echo "  2. Run 'source ~/.bashrc' to update PATH for Bun"
    echo "  3. Navigate to your project directory and run 'bun install'"
    echo "  4. Test the setup with 'bun run dev'"
}

# Show help
show_help() {
    echo "Stock Bot Host Dependencies Setup Script"
    echo ""
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  -h, --help     Show this help message"
    echo "  --skip-docker  Skip Docker installation"
    echo "  --minimal      Install only essential dependencies"
    echo ""
    echo "This script installs all necessary system dependencies for:"
    echo "  - Playwright browser automation"
    echo "  - Docker containers"
    echo "  - Development tools"
    echo "  - Network utilities"
    echo "  - System monitoring tools"
}

# Parse command line arguments
SKIP_DOCKER=false
MINIMAL=false

while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            show_help
            exit 0
            ;;
        --skip-docker)
            SKIP_DOCKER=true
            shift
            ;;
        --minimal)
            MINIMAL=true
            shift
            ;;
        *)
            log_error "Unknown option: $1"
            show_help
            exit 1
            ;;
    esac
done

# Run main function
main
