#!/bin/bash
# Setup tmux layout for Claude Code agent monitoring
# Usage: ./provisioning/tools/setup-tmux-monitoring.sh

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
SESSION_NAME="claude-code-monitor"

echo "🚀 Setting up tmux monitoring layout for Claude Code..."
echo ""

# Check if tmux is installed
if ! command -v tmux &> /dev/null; then
    echo "❌ tmux is not installed. Installing via Homebrew..."
    brew install tmux
fi

# Kill existing session if it exists
tmux kill-session -t $SESSION_NAME 2>/dev/null

# Create new session
tmux new-session -d -s $SESSION_NAME -c "$PROJECT_ROOT"

# Rename first window
tmux rename-window -t $SESSION_NAME:0 'Claude-Monitor'

# Main pane (Claude Code)
tmux send-keys -t $SESSION_NAME:0 "cd $PROJECT_ROOT" C-m
tmux send-keys -t $SESSION_NAME:0 "clear" C-m
tmux send-keys -t $SESSION_NAME:0 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '╔══════════════════════════════════════════════════════════════╗'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '║             Claude Code Main Panel                          ║'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '╚══════════════════════════════════════════════════════════════╝'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '✅ Run your Claude Code commands here:'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '   provisioning/core/cli/provisioning <command>'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '📊 Monitors running in other panes:'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '   • Right pane: Report viewer'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '   • Bottom pane: Agent activity dashboard'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '🔑 Tmux shortcuts:'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '   Ctrl+B then h/j/k/l  → Navigate panes (vim style)'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '   Ctrl+B then [        → Enter scroll/copy mode'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '   Ctrl+B then d        → Detach session'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo '   Mouse: Click and scroll works!'" C-m
tmux send-keys -t $SESSION_NAME:0 "echo ''" C-m

# Split vertically (reports pane on right) - 70% / 30%
tmux split-window -h -t $SESSION_NAME:0 -c "$PROJECT_ROOT" -p 30
tmux send-keys -t $SESSION_NAME:0.1 "cd $PROJECT_ROOT" C-m
tmux send-keys -t $SESSION_NAME:0.1 "clear" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo '╔════════════════════════════════╗'" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo '║      Reports Viewer           ║'" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo '╚════════════════════════════════╝'" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo '📄 View reports with:'" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo 'bat /tmp/<report>.md'" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo 'Or run monitor:'" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo 'nu provisioning/tools/monitor-agents.nu --mode reports'" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo ''" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo '───────────────────────────────'" C-m
tmux send-keys -t $SESSION_NAME:0.1 "echo 'Recent reports:'" C-m
tmux send-keys -t $SESSION_NAME:0.1 "ls /tmp | grep -E '(report|summary|verification)' | tail -5" C-m

# Split the main pane horizontally (agent monitor on bottom) - 70% / 30%
tmux select-pane -t $SESSION_NAME:0.0
tmux split-window -v -t $SESSION_NAME:0.0 -c "$PROJECT_ROOT" -p 35
tmux send-keys -t $SESSION_NAME:0.1 "cd $PROJECT_ROOT" C-m
tmux send-keys -t $SESSION_NAME:0.1 "sleep 1" C-m
tmux send-keys -t $SESSION_NAME:0.1 "nu provisioning/tools/monitor-agents.nu --mode dashboard --refresh 3" C-m

# Select the main pane
tmux select-pane -t $SESSION_NAME:0.0

echo "✅ Tmux session '$SESSION_NAME' created!"
echo ""
echo "📋 Panel Layout:"
echo "   ┌─────────────────────────┬──────────────┐"
echo "   │                         │              │"
echo "   │   Claude Code (Main)    │   Reports    │"
echo "   │                         │   (Viewer)   │"
echo "   │                         │              │"
echo "   ├─────────────────────────┴──────────────┤"
echo "   │                                        │"
echo "   │    Agent Monitor (Auto-refresh)        │"
echo "   │                                        │"
echo "   └────────────────────────────────────────┘"
echo ""
echo "🚀 To attach to the session, run:"
echo "   tmux attach -t $SESSION_NAME"
echo ""
echo "💡 Tmux Quick Reference:"
echo "   Ctrl+B then:                                                   "
echo "   • h/j/k/l     → Navigate panes (vim-style)"
echo "   • Arrow keys  → Navigate panes"
echo "   • [           → Scroll mode (q to exit, use vi keys)"
echo "   • d           → Detach (session keeps running)"
echo "   • z           → Zoom current pane (toggle fullscreen)"
echo "   • x           → Close current pane"
echo ""
echo "🖱️  Mouse Support:"
echo "   • Click to select pane"
echo "   • Scroll wheel works in all panes"
echo "   • Drag border to resize panes"
echo ""
echo "📚 Full tmux config:"
echo "   provisioning/tools/claude-code-tmux.conf"
echo ""

# Auto-attach if run interactively
if [ -t 0 ]; then
    echo "🔗 Attaching to session in 2 seconds... (Ctrl+C to cancel)"
    sleep 2
    tmux attach -t $SESSION_NAME
fi
