#!/usr/bin/env bash
# moctl — miniOrange On-Prem IDP Control CLI
# Installed to: /usr/bin/moctl

set -euo pipefail

MOCTL_DIR="/opt/miniorange/moctl"
VERSION="1.0.0"

############################################################
# Help
############################################################

show_help() {
    cat <<EOF

  moctl v${VERSION} — miniOrange On-Prem IDP Control CLI

  USAGE:
    moctl <command> [subcommand] [options]

  COMMANDS:
    service     Manage services (start, stop, restart, status)
    pre         Check prerequisites and backing services
    log         View service logs
    system      System monitoring (memory, cpu)
    jvm         JVM diagnostics for a service
    diagnose    Run system diagnostics
    version     Show version
    help        Show this help

  EXAMPLES:
    moctl service start              Start all services (ordered)
    moctl service start gatekeeper   Start a single service
    moctl service stop               Stop all services
    moctl service status             Show service status table

    moctl pre                        Check Java, DB, Redis, RabbitMQ

    moctl log miniorange             Last 100 log lines
    moctl log miniorange -f          Follow logs (live tail)
    moctl log miniorange -n 500      Last 500 lines
    moctl log miniorange --since 1h  Logs from last hour

    moctl system memory              Per-service memory usage
    moctl system cpu                 Per-service CPU usage
    moctl system disk                Disk usage

    moctl jvm miniorange             JVM heap, threads, FDs

    moctl diagnose full              Run all diagnostic checks
    moctl diagnose services          Check services only
    moctl diagnose ports             Check port conflicts
    moctl diagnose resources         Check system resources
    moctl diagnose deps              Check dependencies

EOF
}

show_log_help() {
    cat <<EOF
Usage: moctl log <service-name> [options]

Services:
  configserver eurekaserver gatekeeper miniorange
  apps auditing provisioning directory scheduler
  siem workflow idps delivery licensing

Options:
  -f             Follow logs
  -n <lines>     Number of lines
  --since <time> Show logs since a time window

Examples:
  moctl log miniorange
  moctl log miniorange -f
  moctl log miniorange --since 1h
EOF
}

show_jvm_help() {
    cat <<EOF
Usage: moctl jvm <service-name>

Services:
  configserver eurekaserver gatekeeper miniorange
  apps auditing provisioning directory scheduler
  siem workflow idps delivery licensing

Example:
  moctl jvm miniorange
EOF
}

############################################################
# Main Router
############################################################

main() {
    local command=${1:-help}
    shift 2>/dev/null || true

    case "$command" in
        service)
            source "$MOCTL_DIR/services.sh"
            cmd_service "$@"
            ;;
        pre)
            source "$MOCTL_DIR/precheck.sh"
            cmd_pre "$@"
            ;;
        log)
            if [ $# -eq 0 ]; then
                show_log_help
            else
                source "$MOCTL_DIR/logs.sh"
                cmd_log "$@"
            fi
            ;;
        system)
            source "$MOCTL_DIR/system.sh"
            cmd_system "$@"
            ;;
        jvm)
            if [ $# -eq 0 ]; then
                show_jvm_help
            else
                source "$MOCTL_DIR/jvm.sh"
                cmd_jvm "$@"
            fi
            ;;
        diagnose)
            source "$MOCTL_DIR/diagnose.sh"
            cmd_diagnose "$@"
            ;;
        version|--version|-v)
            echo "moctl v${VERSION}"
            ;;
        help|--help|-h)
            show_help
            ;;
        *)
            echo "[moctl] ERROR: Unknown command: $command"
            echo ""
            echo "Run 'moctl help' for usage"
            exit 1
            ;;
    esac
}

main "$@"
