#!/usr/bin/env bash
# ── Timmy Loop tmux Session ────────────────────────────────────────────
# Creates session with 3 panes using standard tmux splits.
#
# Layout:
#   ┌──────────────────────┬──────────────────────┐
#   │    LOOP OUTPUT       │   STATUS DASHBOARD   │
#   ├──────────────────────┤   (live refresh)     │
#   │    HERMES CHAT       │                      │
#   └──────────────────────┴──────────────────────┘
# ───────────────────────────────────────────────────────────────────────

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

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

# Create session — pane 0 starts as shell
tmux new-session -d -s "$SESSION" -x 200 -y 50

# Vertical split: left | right  (Ctrl-b %)
tmux split-window -h -t "$SESSION:0.0"

# Horizontal split on left pane: top-left / bottom-left  (Ctrl-b ")
tmux split-window -v -t "$SESSION:0.0"

# Pane map after splits:
#   0 = top-left      → Loop
#   1 = bottom-left   → Chat  
#   2 = right         → Status

# Set titles
tmux select-pane -t "$SESSION:0.0" -T "Loop"
tmux select-pane -t "$SESSION:0.1" -T "Chat"
tmux select-pane -t "$SESSION:0.2" -T "Status"

# Pane 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 processes
tmux send-keys -t "$SESSION:0.0" "export PATH=\"$HOME/.local/bin:$HOME/.hermes/bin:/usr/local/bin:\$PATH\" && $HOME/.hermes/bin/timmy-loop.sh" Enter
tmux send-keys -t "$SESSION:0.2" "$HOME/.hermes/bin/timmy-status.sh" Enter
tmux send-keys -t "$SESSION:0.1" "cd ~/Timmy-Time-dashboard && hermes" Enter

# Focus chat pane
tmux select-pane -t "$SESSION:0.1"

echo ""
echo "  ┌──────────────────┬──────────────────┐"
echo "  │  Loop (pane 0)   │  Status (pane 2) │"
echo "  ├──────────────────┤                  │"
echo "  │  Chat (pane 1)   │                  │"
echo "  └──────────────────┴──────────────────┘"
echo ""
echo "  Attach:  tmux attach -t timmy-loop"
echo "  Stop:    touch ~/Timmy-Time-dashboard/.loop/STOP"
echo "  Kill:    tmux kill-session -t timmy-loop"
echo ""
