#!/usr/bin/env bash
#
# aos-tmux — ArchitectOS tmux cockpit
#
# Layout:
#   - Session: aos
#   - Window 0:
#       Pane 0: venv + aos-chat
#       Pane 1: tail -F AOS command logs
#       Pane 2: htop (or top)
#
# Usage:
#   chmod +x ~/ArchitectOS/bin/aos-tmux
#   ~/ArchitectOS/bin/aos-tmux
#

set -euo pipefail

# -------------------------------------------------------------------
# Config / auto-detection
# -------------------------------------------------------------------

# Where your project lives (can override by exporting AOS_ROOT)
AOS_ROOT="${AOS_ROOT:-$HOME/ArchitectOS}"
AOS_BIN="$AOS_ROOT/bin"
AOS_CHAT="$AOS_BIN/aos-chat"

# Where AOS runtime state lives (must match aos-chat / aos_rag)
AOS_HOME="${AOS_HOME:-$HOME/.architectos}"

SESSION="aos"
WINDOW="main"

# Try to auto-detect a venv under the project:
detect_venv() {
    local cand
    for cand in ".venv" "venv" "env"; do
        if [ -x "$AOS_ROOT/$cand/bin/python" ]; then
            echo "$AOS_ROOT/$cand"
            return 0
        fi
    done
    return 1
}

VENV_DIR=""
if VENV_DIR=$(detect_venv); then
    :
else
    echo "[aos-tmux] WARNING: No venv detected under $AOS_ROOT (.venv/venv/env)."
    echo "[aos-tmux] You can still use tmux, but aos-chat may fail unless dependencies are installed."
fi

# -------------------------------------------------------------------
# Attach if already running
# -------------------------------------------------------------------

if tmux has-session -t "$SESSION" 2>/dev/null; then
    echo "[aos-tmux] Session '$SESSION' already exists. Attaching..."
    exec tmux attach -t "$SESSION"
fi

# -------------------------------------------------------------------
# Create new session + panes
# -------------------------------------------------------------------

echo "[aos-tmux] Creating new tmux session '$SESSION'..."

# Start detached, create window 0
tmux new-session -d -s "$SESSION" -n "$WINDOW"

# Pane 0: AOS chat client
tmux send-keys -t "$SESSION:0.0" "cd $AOS_ROOT" C-m
tmux send-keys -t "$SESSION:0.0" "export AOS_HOME=\"$AOS_HOME\"" C-m

if [ -n "$VENV_DIR" ]; then
    tmux send-keys -t "$SESSION:0.0" "source \"$VENV_DIR/bin/activate\"" C-m
fi

tmux send-keys -t "$SESSION:0.0" "$AOS_CHAT" C-m

# Pane 1: logs tail (split horizontally from pane 0)
tmux split-window -h -t "$SESSION:0.0"
tmux send-keys -t "$SESSION:0.1" "mkdir -p \"$AOS_HOME/logs/commands\"" C-m
tmux send-keys -t "$SESSION:0.1" "cd \"$AOS_HOME/logs/commands\"" C-m
tmux send-keys -t "$SESSION:0.1" 'echo "[aos-tmux] Tailing command logs..."; tail -F *.log 2>/dev/null' C-m

# Pane 2: system monitor (split vertically from pane 1 or 0; we'll do from 1)
tmux select-pane -t "$SESSION:0.1"
tmux split-window -v -t "$SESSION:0.1"

tmux send-keys -t "$SESSION:0.2" 'cd ~' C-m
tmux send-keys -t "$SESSION:0.2" 'if command -v htop >/dev/null 2>&1; then echo "[aos-tmux] Starting htop..."; htop; else echo "[aos-tmux] htop not found, running top"; top; fi' C-m

# Optional: choose a nice layout (main-horizontal or tiled)
# main-horizontal = big pane on top, two small on bottom
tmux select-layout -t "$SESSION:0" main-horizontal

# Focus back on aos-chat pane
tmux select-pane -t "$SESSION:0.0"

# Attach
exec tmux attach -t "$SESSION"
