#!/bin/bash
# Foliko Installer

set -e

echo -e "\033[36mFoliko Installer\033[0m"
echo -e "\033[36m=================\033[0m"
echo ""

# Detect OS
detect_os() {
    if [[ "$OSTYPE" == "darwin"* ]]; then
        echo "macOS"
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        echo "Linux"
    else
        echo "unknown"
    fi
}

OS=$(detect_os)

# ============ Check/Install Node.js ============
echo -e "\033[36mChecking Node.js...\033[0m"
if command -v node &> /dev/null; then
    echo -e "\033[32mNode.js installed: $(node --version)\033[0m"
else
    echo -e "\033[33mNode.js not found, installing...\033[0m"

    if [[ "$OS" == "macOS" ]]; then
        if command -v brew &> /dev/null; then
            brew install node
        else
            echo -e "\033[31mHomebrew not found. Install it first:\033[0m"
            echo "/bin/bash -c \"\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)\""
            exit 1
        fi
    elif [[ "$OS" == "Linux" ]]; then
        if command -v apt-get &> /dev/null; then
            sudo apt-get update && sudo apt-get install -y nodejs npm
        elif command -v yum &> /dev/null; then
            sudo yum install -y nodejs npm
        elif command -v pacman &> /dev/null; then
            sudo pacman -S nodejs npm
        else
            echo -e "\033[31mNo package manager found. Install Node.js manually: https://nodejs.org/\033[0m"
            exit 1
        fi
    fi

    if command -v node &> /dev/null; then
        echo -e "\033[32mNode.js installed: $(node --version)\033[0m"
    else
        echo -e "\033[31mNode.js installation failed\033[0m"
        exit 1
    fi
fi
echo ""

# ============ Check/Install Python ============
echo -e "\033[36mChecking Python...\033[0m"
if command -v python3 &> /dev/null; then
    echo -e "\033[32mPython installed: $(python3 --version)\033[0m"
else
    echo -e "\033[33mPython not found, installing...\033[0m"

    if [[ "$OS" == "macOS" ]]; then
        if command -v brew &> /dev/null; then
            brew install python3
        fi
    elif [[ "$OS" == "Linux" ]]; then
        if command -v apt-get &> /dev/null; then
            sudo apt-get update && sudo apt-get install -y python3 python3-venv python3-pip
        elif command -v yum &> /dev/null; then
            sudo yum install -y python3
        elif command -v pacman &> /dev/null; then
            sudo pacman -S python python-pip
        fi
    fi

    if command -v python3 &> /dev/null; then
        echo -e "\033[32mPython installed: $(python3 --version)\033[0m"
    else
        echo -e "\033[33mPython installation failed (optional)\033[0m"
    fi
fi
echo ""

# ============ Check/Install uv ============
echo -e "\033[36mChecking uv...\033[0m"
if command -v uv &> /dev/null; then
    echo -e "\033[32muv installed: $(uv --version)\033[0m"
else
    echo -e "\033[33muv not found, installing...\033[0m"

    if [[ "$OS" == "macOS" ]] || [[ "$OS" == "Linux" ]]; then
        curl -LsSf https://astral.sh/uv/install.sh | sh
        source $HOME/.local/bin/env 2>/dev/null || true

        if command -v uv &> /dev/null; then
            echo -e "\033[32muv installed: $(uv --version)\033[0m"
        else
            echo -e "\033[33muv installation failed (optional)\033[0m"
        fi
    fi
fi
echo ""

# ============ Install Foliko ============
echo -e "\033[36mInstalling Foliko...\033[0m"
npm install -g foliko

if command -v foliko &> /dev/null; then
    echo ""
    echo -e "\033[32mInstallation complete!\033[0m"
    echo "Run: foliko chat"
else
    echo ""
    echo -e "\033[31mInstallation failed. Try manually: npm install -g foliko\033[0m"
    exit 1
fi
