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

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 (50%)
tmux split-window -h -p 50 -t "$SESSION:0.0"

# Horizontal split on left pane: Loop (small top) / Status (big bottom)
# Loop gets ~14% height (10 rows out of ~62), Status gets the rest
tmux split-window -v -p 83 -t "$SESSION:0.0"

# Pane map after splits:
#   0 = top-left (small)   → Loop output
#   1 = bottom-left (big)  → Status dashboard
#   2 = 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 "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" "cd ~/Timmy-Time-dashboard && hermes" Enter

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

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