#!/usr/bin/env bash
# jw - JJ Workspace management tool (inspired by worktrunk)
# https://github.com/max-sixty/worktrunk
#
# Designed for running AI agents in parallel using jj workspaces.
# Uses gum for interactive prompts and styled output.
#
# Core commands:
#   jw switch <name>         Switch to workspace (creates if needed)
#   jw switch -c <name>      Create new workspace
#   jw switch -c -x <cmd>    Create and execute command (claude, opencode, etc)
#   jw list                  List all workspaces with status
#   jw select                Interactive workspace picker
#   jw remove [name]         Remove workspace (current if no name)
#   jw merge [name]          Merge workspace changes to trunk
#
# See: jw help

set -euo pipefail

# Determine script directory for sourcing lib files
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LIB_DIR="${SCRIPT_DIR}/lib"

# Source library files
source "${LIB_DIR}/common.sh"
source "${LIB_DIR}/switch.sh"
source "${LIB_DIR}/list.sh"
source "${LIB_DIR}/select.sh"
source "${LIB_DIR}/remove.sh"
source "${LIB_DIR}/merge.sh"
source "${LIB_DIR}/sync.sh"
source "${LIB_DIR}/help.sh"

# =============================================================================
# Main
# =============================================================================

main() {
    local cmd="${1:-help}"
    shift || true
    
    case "$cmd" in
        switch|s)
            if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
                cmd_switch_help
            else
                cmd_switch "$@"
            fi
            ;;
        create|c)
            if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
                cmd_switch_help  # Same as switch
            else
                cmd_create "$@"
            fi
            ;;
        list|ls|l)
            if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
                cmd_list_help
            else
                cmd_list "$@"
            fi
            ;;
        select|sel)
            if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
                cmd_select_help
            else
                cmd_select "$@"
            fi
            ;;
        remove|rm|r)
            if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
                cmd_remove_help
            else
                cmd_remove "$@"
            fi
            ;;
        merge|m)
            if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
                cmd_merge_help
            else
                cmd_merge "$@"
            fi
            ;;
        sync)
            if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
                cmd_sync_help
            else
                cmd_sync "$@"
            fi
            ;;
        help|--help|-h)
            cmd_help
            ;;
        *)
            _error "Unknown command: $cmd"
            _info "Run 'jw help' for usage"
            exit 1
            ;;
    esac
}

main "$@"
