#!/bin/bash
# Setup iTerm2 splits for Claude Code agent monitoring
# Usage: ./provisioning/tools/setup-iterm-monitoring.sh

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"

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

# AppleScript to create iTerm splits
osascript <<EOF
tell application "iTerm"
    activate

    -- Create new window
    create window with default profile

    tell current session of current window
        -- Main pane (Claude Code runs here)
        set name to "Claude Code"
        write text "cd $PROJECT_ROOT"
        write text "echo ''"
        write text "echo '✅ Main Panel: Run Claude Code here'"
        write text "echo '   Commands like: provisioning/core/cli/provisioning <command>'"
        write text "echo ''"
    end tell

    -- Split horizontally (monitoring dashboard on bottom)
    tell current session of current window
        -- Split horizontally (top/bottom)
        set monitoring_pane to (split horizontally with default profile)
    end tell

    tell monitoring_pane
        set name to "Agent Monitor"
        write text "cd $PROJECT_ROOT"
        write text "echo ''"
        write text "echo '📊 Monitoring Panel: Agent activity'"
        write text "echo '   Running: nu provisioning/tools/monitor-agents.nu --mode dashboard'"
        write text "echo ''"
        write text "sleep 2"
        write text "nu provisioning/tools/monitor-agents.nu --mode dashboard"
    end tell

    -- Split the top pane vertically (reports on right)
    tell first session of current tab of current window
        set reports_pane to (split vertically with default profile)
    end tell

    tell reports_pane
        set name to "Reports"
        write text "cd $PROJECT_ROOT"
        write text "echo ''"
        write text "echo '📄 Reports Panel: Real-time report viewer'"
        write text "echo '   Monitoring /tmp for new reports...'"
        write text "echo ''"
        write text "echo 'Available commands:'"
        write text "echo '  - bat /tmp/<report>.md  # View specific report'"
        write text "echo '  - nu provisioning/tools/monitor-agents.nu --mode reports'"
        write text "echo ''"
    end tell

    -- Resize panes for better visibility
    tell current window
        -- Make bottom pane smaller (monitoring dashboard)
        tell monitoring_pane
            -- Set height to about 30% of window
        end tell
    end tell

    -- Select the main Claude Code pane
    tell current window
        select first session of current tab
    end tell

end tell
EOF

echo ""
echo "✅ iTerm2 monitoring layout created!"
echo ""
echo "📋 Panel Layout:"
echo "   ┌─────────────────┬──────────────────┐"
echo "   │                 │                  │"
echo "   │  Claude Code    │    Reports       │"
echo "   │  (Main)         │    (Viewer)      │"
echo "   │                 │                  │"
echo "   ├─────────────────┴──────────────────┤"
echo "   │                                    │"
echo "   │     Agent Monitor (Dashboard)      │"
echo "   │                                    │"
echo "   └────────────────────────────────────┘"
echo ""
echo "💡 Tips:"
echo "   • Top left: Run your Claude Code commands here"
echo "   • Top right: View reports with 'bat /tmp/<file>.md'"
echo "   • Bottom: Auto-refreshing agent activity monitor"
echo ""
echo "🎯 Navigation:"
echo "   • Cmd+Option+Arrow Keys: Switch between panes"
echo "   • Cmd+D: New vertical split"
echo "   • Cmd+Shift+D: New horizontal split"
echo ""
