#!/usr/bin/env bash
set -euo pipefail

VERSION="0.1.0"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB_DIR="$(cd "$SCRIPT_DIR/../lib" && pwd)"

source "$LIB_DIR/state.sh"
source "$LIB_DIR/core.sh"
source "$LIB_DIR/pane.sh"
source "$LIB_DIR/wait.sh"
source "$LIB_DIR/context.sh"
source "$LIB_DIR/repl.sh"
source "$LIB_DIR/highlevel.sh"

show_help() {
    cat << 'EOF'
tmux-ctl - Terminal multiplexer control CLI

USAGE:
    tmux-ctl <command> [options]

HIGH-LEVEL COMMANDS:
    eval <cmd>                       Run command, return output, auto-cleanup
    repl <type> [cmd]                Start REPL (python/node/psql/etc)
    exec <cmd>                       Execute in current context
    close                            Close current context
    use <name>                       Switch to named process

    start <cmd> --name=<name>        Start long-running process
             [--wait=<pattern>]
    stop <name...>                   Stop process(es) by name
    ps                               List running processes
    logs <name> [--tail=N]           View process logs
    wait <name...>                   Wait for process(es) to finish

LOW-LEVEL COMMANDS:
    launch <cmd>                     Launch command in new pane
    send <text> --pane=<id>          Send text to pane
    capture --pane=<id>              Capture pane output
    list-panes                       List all panes (JSON)
    kill --pane=<id>                 Kill pane
    interrupt --pane=<id>            Send Ctrl+C to pane
    escape --pane=<id>               Send Escape to pane
    wait-idle --pane=<id>            Wait until pane is idle
    wait-for <pattern> --pane=<id>   Wait for pattern

SYSTEM COMMANDS:
    status                           Show current tmux status
    state                            Show current state
    help                             Show this help
    version                          Show version

EXAMPLES:
    # Simple evaluation
    tmux-ctl eval "npm test"

    # REPL one-shot
    tmux-ctl repl python "2+2"

    # REPL session
    tmux-ctl repl python
    tmux-ctl exec "import sys"
    tmux-ctl exec "print(sys.version)"
    tmux-ctl close

    # Parallel execution
    tmux-ctl start "npm run dev" --name=server --wait="Server started"
    tmux-ctl start "npm test -- --watch" --name=tests
    tmux-ctl ps
    tmux-ctl logs server
    tmux-ctl stop server tests
EOF
}

main() {
    if [[ $# -eq 0 ]]; then
        show_help
        exit 0
    fi

    local cmd="$1"
    shift

    case "$cmd" in
        help|--help|-h)
            show_help
            ;;
        version|--version|-v)
            echo "tmux-ctl v${VERSION}"
            ;;
        eval)
            if [[ $# -eq 0 ]]; then
                echo "Error: eval requires a command argument" >&2
                exit 1
            fi
            cmd_eval "$*"
            ;;
        repl)
            if [[ $# -eq 0 ]]; then
                echo "Error: repl requires a type (python/node/psql/etc)" >&2
                exit 1
            fi
            local repl_type="$1"
            shift
            if [[ $# -gt 0 ]]; then
                # One-shot mode
                repl_oneshot "$repl_type" "$*"
            else
                # Session mode
                repl_start "$repl_type"
            fi
            ;;
        exec)
            if [[ $# -eq 0 ]]; then
                echo "Error: exec requires a command argument" >&2
                exit 1
            fi
            repl_exec "$*"
            ;;
        close)
            repl_close
            ;;
        use)
            if [[ $# -eq 0 ]]; then
                echo "Error: use requires a process name" >&2
                exit 1
            fi
            cmd_use "$1"
            ;;
        start)
            local command="" name="" wait_pattern="" timeout="30"
            while [[ $# -gt 0 ]]; do
                case "$1" in
                    --name=*)
                        name="${1#--name=}"
                        shift
                        ;;
                    --wait=*)
                        wait_pattern="${1#--wait=}"
                        shift
                        ;;
                    --timeout=*)
                        timeout="${1#--timeout=}"
                        shift
                        ;;
                    *)
                        if [[ -z "$command" ]]; then
                            command="$1"
                        else
                            command="$command $1"
                        fi
                        shift
                        ;;
                esac
            done
            if [[ -z "$command" ]]; then
                echo "Error: start requires a command" >&2
                exit 1
            fi
            cmd_start "$command" "$name" "$wait_pattern" "$timeout"
            ;;
        stop)
            if [[ $# -eq 0 ]]; then
                echo "Error: stop requires at least one process name" >&2
                exit 1
            fi
            cmd_stop "$@"
            ;;
        ps)
            cmd_ps
            ;;
        logs)
            if [[ $# -eq 0 ]]; then
                echo "Error: logs requires a process name" >&2
                exit 1
            fi
            local name="$1"
            local tail_lines=""
            shift
            for arg in "$@"; do
                if [[ "$arg" == --tail=* ]]; then
                    tail_lines="${arg#--tail=}"
                fi
            done
            cmd_logs "$name" "$tail_lines"
            ;;
        wait)
            if [[ $# -eq 0 ]]; then
                echo "Error: wait requires at least one process name" >&2
                exit 1
            fi
            cmd_wait "$@"
            ;;
        state)
            state_init
            state_read | jq .
            ;;
        status)
            state_init
            if tmux_is_inside; then
                echo "Mode: local (inside tmux)"
                echo "Session: $(tmux_get_current_session)"
                echo "Window: $(tmux_get_current_window)"
                echo "Pane: $(tmux_get_current_pane)"
            else
                echo "Mode: remote (outside tmux)"
                local sessions
                sessions=$(state_get_sessions)
                if [[ -n "$sessions" ]]; then
                    echo "Tracked sessions:"
                    while IFS= read -r sess; do
                        echo "  - $sess"
                    done <<< "$sessions"
                else
                    echo "No tracked sessions"
                fi
            fi
            ;;
        launch)
            if [[ $# -eq 0 ]]; then
                echo "Error: launch requires a command argument" >&2
                exit 1
            fi
            pane_launch "$*"
            ;;
        capture)
            local pane_id=""
            for arg in "$@"; do
                if [[ "$arg" == --pane=* ]]; then
                    pane_id="${arg#--pane=}"
                fi
            done
            if [[ -z "$pane_id" ]]; then
                echo "Error: --pane=<id> required" >&2
                exit 1
            fi
            pane_capture "$pane_id"
            ;;
        list-panes)
            pane_list
            ;;
        send)
            local text="" pane_id="" enter="true"
            for arg in "$@"; do
                if [[ "$arg" == --pane=* ]]; then
                    pane_id="${arg#--pane=}"
                elif [[ "$arg" == --no-enter ]]; then
                    enter="false"
                elif [[ -z "$text" ]]; then
                    text="$arg"
                fi
            done
            if [[ -z "$text" || -z "$pane_id" ]]; then
                echo "Error: send requires <text> and --pane=<id>" >&2
                exit 1
            fi
            pane_send "$text" "$pane_id" "$enter"
            ;;
        kill)
            local pane_id=""
            for arg in "$@"; do
                if [[ "$arg" == --pane=* ]]; then
                    pane_id="${arg#--pane=}"
                fi
            done
            if [[ -z "$pane_id" ]]; then
                echo "Error: --pane=<id> required" >&2
                exit 1
            fi
            pane_kill "$pane_id"
            ;;
        interrupt)
            local pane_id=""
            for arg in "$@"; do
                if [[ "$arg" == --pane=* ]]; then
                    pane_id="${arg#--pane=}"
                fi
            done
            if [[ -z "$pane_id" ]]; then
                echo "Error: --pane=<id> required" >&2
                exit 1
            fi
            pane_interrupt "$pane_id"
            ;;
        escape)
            local pane_id=""
            for arg in "$@"; do
                if [[ "$arg" == --pane=* ]]; then
                    pane_id="${arg#--pane=}"
                fi
            done
            if [[ -z "$pane_id" ]]; then
                echo "Error: --pane=<id> required" >&2
                exit 1
            fi
            pane_escape "$pane_id"
            ;;
        wait-idle)
            local pane_id="" idle_time="2" timeout="30"
            for arg in "$@"; do
                if [[ "$arg" == --pane=* ]]; then
                    pane_id="${arg#--pane=}"
                elif [[ "$arg" == --idle-time=* ]]; then
                    idle_time="${arg#--idle-time=}"
                elif [[ "$arg" == --timeout=* ]]; then
                    timeout="${arg#--timeout=}"
                fi
            done
            if [[ -z "$pane_id" ]]; then
                echo "Error: --pane=<id> required" >&2
                exit 1
            fi
            wait_idle "$pane_id" "$idle_time" 0.5 "$timeout"
            ;;
        wait-for)
            if [[ $# -eq 0 ]]; then
                echo "Error: wait-for requires a pattern argument" >&2
                exit 1
            fi
            local pattern="$1"
            shift
            local pane_id="" timeout="30"
            for arg in "$@"; do
                if [[ "$arg" == --pane=* ]]; then
                    pane_id="${arg#--pane=}"
                elif [[ "$arg" == --timeout=* ]]; then
                    timeout="${arg#--timeout=}"
                fi
            done
            if [[ -z "$pane_id" ]]; then
                echo "Error: --pane=<id> required" >&2
                exit 1
            fi
            wait_for_pattern "$pattern" "$pane_id" "$timeout"
            ;;
        *)
            echo "Error: Unknown command '$cmd'"
            echo "Run 'tmux-ctl help' for usage"
            exit 1
            ;;
    esac
}

main "$@"
