#!/usr/bin/env bash
# ── Timmy Loop tmux Session ────────────────────────────────────────────
# Creates session with 4 panes.
#
# Layout (245×62 terminal):
#   ┌────────────────────────────────────────────┬──────────────────────┐
#   │              LOOP (10 rows)                │                      │
#   ├──────────────────────┬─────────────────────┤   CHAT (full height) │
#   │    STATUS (81 cols)  │  LOOPSTAT (40 cols) │                      │
#   │    (50 rows)         │  (50 rows)          │                      │
#   └──────────────────────┴─────────────────────┴──────────────────────┘
# ───────────────────────────────────────────────────────────────────────

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 245 -y 62

# Vertical split: left (~50%) | right Chat (~50%)
tmux split-window -h -p 50 -t "$SESSION:0.0"

# Horizontal split on left: Loop (small top ~16%) | bottom (~84%)
tmux split-window -v -p 83 -t "$SESSION:0.0"

# Vertical split on bottom-left: Status (wide ~67%) | LOOPSTAT (~33%)
tmux split-window -h -p 33 -t "$SESSION:0.1"

# Pane map after splits:
#   0 = top-left (full width) → Loop output
#   1 = bottom-left (wide)    → Status dashboard
#   2 = bottom-mid (narrow)   → LOOPSTAT (strategy)
#   3 = right (full height)   → Hermes chat

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

# 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.1" "$HOME/.hermes/bin/timmy-status.sh" Enter
tmux send-keys -t "$SESSION:0.2" "$HOME/.hermes/bin/timmy-loopstat.sh" Enter
tmux send-keys -t "$SESSION:0.3" "cd ~/Timmy-Time-dashboard && hermes" Enter

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

echo ""
echo "  ┌──────────────────────────────────┬──────────────────┐"
echo "  │         Loop (pane 0)            │                  │"
echo "  ├────────────────────┬─────────────┤  Chat (pane 3)   │"
echo "  │  Status (pane 1)   │ LOOPSTAT(2) │                  │"
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 ""
