#!/bin/bash
set -e

PORT=9105
HOST=${1:-localhost}
RED='\033[0;31m'; GREEN='\033[0;32m'; BLUE='\033[0;34m'; YELLOW='\033[0;33m'; NC='\033[0m'

command_exists() { command -v "$1" >/dev/null 2>&1; }
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

# --- 1. BOOTSTRAP ---
if [ ! -d "$SCRIPT_DIR/.git" ] && [[ ! "$(pwd)" =~ "opencortex" ]]; then
    echo -e "${BLUE}=== OpenCortex: Zero-to-One Bootstrapper ===${NC}"
    git clone http://10.10.10.201:3001/amr/opencortex.git opencortex
    cd opencortex && git submodule update --init --recursive
    exec ./opencortex.sh "$@"
fi

# --- 2. SETUP ---
setup_system() {
    echo -e "${BLUE}=== OpenCortex: Initializing System ===${NC}"
    cd "$SCRIPT_DIR"
    if [ ! -f .env ]; then
        cp .env.example .env
        
        echo -e "\n${YELLOW}--- Identity Configuration ---${NC}"
        echo "Let's personalize your OpenCortex experience."
        read -p "Your Name [User]: " user_name < /dev/tty
        user_name=${user_name:-User}
        sed -i "s|MEMEX_USER=.*|MEMEX_USER=\"$user_name\"|" .env
        
        read -p "Agent Name [OpenCortex]: " agent_name < /dev/tty
        agent_name=${agent_name:-OpenCortex}
        sed -i "s|MEMEX_ASSISTANT=.*|MEMEX_ASSISTANT=\"$agent_name\"|" .env
        
        echo -e "\n${YELLOW}--- LLM Configuration ---${NC}"
        echo "You can enter your LLM API keys now, or press Enter to skip and configure them later."
        read -p "Gemini API Key: " gemini_key < /dev/tty
        [ -n "$gemini_key" ] && sed -i "s|GEMINI_API_KEY=.*|GEMINI_API_KEY=\"$gemini_key\"|" .env
        read -p "Anthropic API Key: " anthropic_key < /dev/tty
        [ -n "$anthropic_key" ] && sed -i "s|ANTHROPIC_API_KEY=.*|ANTHROPIC_API_KEY=\"$anthropic_key\"|" .env
        read -p "OpenAI API Key: " openai_key < /dev/tty
        [ -n "$openai_key" ] && sed -i "s|OPENAI_API_KEY=.*|OPENAI_API_KEY=\"$openai_key\"|" .env

        echo -e "\n${YELLOW}--- Memex Folder Structure ---${NC}"
        echo "Enter the absolute paths for your existing folder structure (press Enter to accept default)."
        read -p "Memex Root [/memex]: " memex_dir < /dev/tty
        memex_dir=${memex_dir:-/memex}
        sed -i "s|MEMEX_DIR=.*|MEMEX_DIR=\"$memex_dir\"|" .env
        sed -i "s|\"/memex/|\"$memex_dir/|g" .env
        
        read -p "Inbox Directory [$memex_dir/inbox]: " inbox_dir < /dev/tty
        inbox_dir=${inbox_dir:-$memex_dir/inbox}
        sed -i "s|INBOX_DIR=.*|INBOX_DIR=\"$inbox_dir\"|" .env
        
        read -p "Daily Directory [$memex_dir/daily]: " daily_dir < /dev/tty
        daily_dir=${daily_dir:-$memex_dir/daily}
        sed -i "s|DAILY_DIR=.*|DAILY_DIR=\"$daily_dir\"|" .env
        
        read -p "Projects Directory [$memex_dir/projects]: " proj_dir < /dev/tty
        proj_dir=${proj_dir:-$memex_dir/projects}
        sed -i "s|PROJECTS_DIR=.*|PROJECTS_DIR=\"$proj_dir\"|" .env
    fi

    mkdir -p src
    for f in literate/*.org; do
        emacs --batch --eval "(require 'org)" --eval "(org-babel-tangle-file \"$f\")" >/dev/null 2>&1 || true
    done
    mkdir -p "$HOME/.local/bin"
    ln -sf "$SCRIPT_DIR/opencortex.sh" "$HOME/.local/bin/opencortex"
    echo -e "${GREEN}✓ Setup complete. You can now run 'opencortex tui'.${NC}"
}

if [ ! -f "$SCRIPT_DIR/src/package.lisp" ] || [ ! -f "$SCRIPT_DIR/.env" ]; then
    setup_system
fi

# --- 3. BOOT ---
if [[ "$1" == "--boot" ]]; then
    if [ -f "$SCRIPT_DIR/.env" ]; then
        while IFS='=' read -r key value || [ -n "$key" ]; do
          if [[ $key =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then
            val=$(echo "$value" | sed 's/^"//;s/"$//')
            export "$key=$val"
          fi
        done < "$SCRIPT_DIR/.env"
    fi
    exec sbcl --non-interactive \
         --eval "(load \"~/quicklisp/setup.lisp\")" \
         --eval "(push \"$SCRIPT_DIR/\" asdf:*central-registry*)" \
         --eval "(ql:quickload :opencortex)" \
         --eval "(opencortex:main)"
fi

# --- 4. INTERACT ---
if [[ "$1" == "tui" ]]; then
    # Ensure daemon is running
    if ! (nc -z $HOST $PORT 2>/dev/null || (command_exists socat && socat - TCP:$HOST:$PORT,connect-timeout=1 2>/dev/null)); then
        echo -e "${YELLOW}Brain is offline. Awakening...${NC}"
        "$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &
        for i in {1..15}; do
            sleep 2
            if nc -z $HOST $PORT 2>/dev/null || (command_exists socat && socat - TCP:$HOST:$PORT,connect-timeout=1 2>/dev/null); then break; fi
            echo -n "."
        done
        echo ""
    fi
    
    # Launch TUI
    echo -e "${BLUE}Launching Croatoan TUI...${NC}"
    exec sbcl --non-interactive \
         --load ~/quicklisp/setup.lisp \
         --eval "(push (truename \"$SCRIPT_DIR/\") asdf:*central-registry*)" \
         --eval "(ql:quickload :opencortex/tui :silent t)" \
         --eval "(opencortex.tui:main)"
fi

connect() {
    if command_exists socat && socat - TCP:$HOST:$PORT,connect-timeout=1 2>/dev/null; then
        socat - TCP:$HOST:$PORT
        return 0
    elif command_exists nc && nc -z $HOST $PORT 2>/dev/null; then
        nc $HOST $PORT
        return 0
    fi
    return 1
}

if connect; then exit 0; fi

echo -e "${YELLOW}Brain is offline. Awakening...${NC}"
"$SCRIPT_DIR/opencortex.sh" --boot > "$SCRIPT_DIR/brain.log" 2>&1 &

for i in {1..15}; do
    sleep 2
    if connect; then exit 0; fi
    echo -n "."
done

echo -e "${RED}\n✗ Failed to connect to brain.${NC}"
exit 1
