#!/usr/bin/env bash
# Start the timmy-loop tmux dashboard with ONLY zero-cost panes active.
# Loop (pane 1) and Chat (pane 4) are held — they hit Claude API.

SESSION="timmy-loop"
export PATH="$HOME/.local/bin:$HOME/.hermes/bin:/usr/local/bin:$PATH"

# Kill existing
tmux kill-session -t "$SESSION" 2>/dev/null
sleep 1

# Create session (pane-base-index=1, base-index=1)
tmux new-session -d -s "$SESSION" -x 245 -y 62

# Get window index
WIN=$(tmux list-windows -t "$SESSION" -F '#{window_index}' | head -1)

# Vertical split: left (~50%) | right Chat (~50%)
# After: pane 1 = left, pane 2 = right
tmux split-window -h -p 50 -t "${SESSION}:${WIN}.1"

# Horizontal split on left pane 1: Loop (small top) | bottom
# After: pane 1 = top-left (loop), pane 2 = bottom-left, pane 3 = right
tmux split-window -v -p 83 -t "${SESSION}:${WIN}.1"

# Vertical split on bottom-left (pane 2): Status | LOOPSTAT
# After: pane 1 = top-left (loop), pane 2 = bottom-left (status), 
#        pane 3 = bottom-mid (loopstat), pane 4 = right (chat)
tmux split-window -h -p 33 -t "${SESSION}:${WIN}.2"

# Set titles
tmux select-pane -t "${SESSION}:${WIN}.1" -T "Loop (HELD)"
tmux select-pane -t "${SESSION}:${WIN}.2" -T "Status"
tmux select-pane -t "${SESSION}:${WIN}.3" -T "LOOPSTAT"
tmux select-pane -t "${SESSION}:${WIN}.4" -T "Chat (ready)"

# Border styling
tmux set-option -t "$SESSION" pane-border-status top
tmux set-option -t "$SESSION" pane-border-format " #{pane_title} "
tmux set-option -t "$SESSION" pane-border-style "fg=colour240"
tmux set-option -t "$SESSION" pane-active-border-style "fg=cyan"

# Start ONLY zero-cost panes (pure bash, no API calls)
tmux send-keys -t "${SESSION}:${WIN}.2" "$HOME/.hermes/bin/timmy-status.sh" Enter
tmux send-keys -t "${SESSION}:${WIN}.3" "$HOME/.hermes/bin/timmy-loopstat.sh" Enter

# Held panes - just messages
tmux send-keys -t "${SESSION}:${WIN}.1" "echo 'LOOP HELD - run ~/.hermes/bin/timmy-loop.sh when ready'" Enter
tmux send-keys -t "${SESSION}:${WIN}.4" "cd ~/Timmy-Time-dashboard" Enter

# Focus chat pane
tmux select-pane -t "${SESSION}:${WIN}.4"

echo ""
echo "  Dashboard started: tmux attach -t $SESSION"
echo "  Pane 2 (Status) + Pane 3 (LOOPSTAT) running — zero API cost"
echo "  Pane 1 (Loop) + Pane 4 (Chat) HELD — would hit Claude API"
echo ""
