#!/usr/bin/env bash

set -e

SCRIPT_DIR="$(cd "$(dirname "$(readlink -f "$0")")" && pwd)"
ROOT="$(dirname "$SCRIPT_DIR")"
VERSION="$(tr -d '[:space:]' < "$ROOT/VERSION")"
PYTHON="$ROOT/.venv/bin/python"
if [[ ! -x "$PYTHON" ]]; then
    PYTHON="$(command -v python3 || true)"
fi

# Formatting
BOLD=$(tput bold 2>/dev/null || true)
NORMAL=$(tput sgr0 2>/dev/null || true)
MAROON='\033[38;5;1m'
RED='\033[31m'
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
CYAN='\033[36m'

show_help() {
    echo -e "${BOLD}${MAROON}LANimals Command Center${NORMAL}"
    echo "Local Network Intelligence Platform"
    echo ""
    echo -e "${BOLD}${MAROON}╔═══════════════════════════════════════════════╗${NORMAL}"
    echo -e "${BOLD}${MAROON}║          SUPPORTED APPLIANCE COMMANDS         ║${NORMAL}"
    echo -e "${BOLD}${MAROON}╚═══════════════════════════════════════════════╝${NORMAL}"
    echo -e "  ${BOLD}dash${NORMAL}      Show LANimals dashboard"
    echo -e "  ${BOLD}start${NORMAL}     Start the local browser appliance"
    echo -e "  ${BOLD}stop${NORMAL}      Stop the local browser appliance"
    echo -e "  ${BOLD}status${NORMAL}    Show local appliance health"
    echo -e "  ${BOLD}open${NORMAL}      Open the running browser console"
    echo -e "  ${BOLD}setup${NORMAL}     Approve the private LAN scan scope"
    echo -e "  ${BOLD}doctor${NORMAL}    Check dependencies and configuration"
    echo -e "  ${BOLD}config${NORMAL}    Show the active LANimals configuration"
    echo -e "  ${BOLD}sysinfo${NORMAL}   Analyze local system components"
    echo ""
    echo -e "${BOLD}${YELLOW}╔═══════════════════════════════════════════════╗${NORMAL}"
    echo -e "${BOLD}${YELLOW}║      EXPERIMENTAL / LEGACY RESEARCH TOOLS     ║${NORMAL}"
    echo -e "${BOLD}${YELLOW}╚═══════════════════════════════════════════════╝${NORMAL}"
    echo "  Preserved for research lineage. These commands are not"
    echo "  automatically covered by the v2.1 appliance contract."
    echo ""
    echo -e "  ${BOLD}recon${NORMAL}     Run autonomous recon"
    echo -e "  ${BOLD}alert${NORMAL}     Run threat alert system"
    echo -e "  ${BOLD}monitor${NORMAL}   Start continuous monitoring"
    echo -e "  ${BOLD}traffic${NORMAL}   Analyze network traffic"
    echo -e "  ${BOLD}netmap${NORMAL}    Map network devices visually"
    echo -e "  ${BOLD}viznet${NORMAL}    Interactive network visualization"
    echo -e "  ${BOLD}fortress${NORMAL}  Security hardening toolkit"
    echo -e "  ${BOLD}hunter${NORMAL}    Threat hunting and detection"
    echo -e "  ${BOLD}threats${NORMAL}   Show recent threats"
    echo -e "  ${BOLD}vulnscan${NORMAL}  Network vulnerability scanner"
    echo ""
    echo -e "${BOLD}${MAROON}╔═══════════════════════════════════════════════╗${NORMAL}"
    echo -e "${BOLD}${MAROON}║               GENERAL COMMANDS                ║${NORMAL}"
    echo -e "${BOLD}${MAROON}╚═══════════════════════════════════════════════╝${NORMAL}"
    echo -e "  ${BOLD}help${NORMAL}      Show this help message"
    echo -e "  ${BOLD}version${NORMAL}   Show LANimals version"
    echo -e "  ${BOLD}update${NORMAL}    Show update guidance"
    echo ""
    echo -e "For command-specific help, use: ${BOLD}lanimals [command] --help${NORMAL}"
}

# Handle command line arguments
case "$1" in
    dash|dashboard)
        shift
        if [[ -z "$PYTHON" ]]; then
            echo "Python 3 is required for the LANimals browser appliance." >&2
            exit 2
        fi
        exec "$PYTHON" -m core.appliance start "$@"
        ;;
    start|stop|status|open|setup|doctor|config)
        command="$1"
        shift
        if [[ -z "$PYTHON" ]]; then
            echo "Python 3 is required for 'lanimals $command'." >&2
            exit 2
        fi
        exec "$PYTHON" -m core.appliance "$command" "$@"
        ;;
    recon)
        shift
        exec "$SCRIPT_DIR/lanimals_recon" "$@"
        ;;
    alert)
        shift
        exec "$SCRIPT_DIR/lanimals_alert" "$@"
        ;;
    monitor)
        shift
        exec "$SCRIPT_DIR/lanimals_alert" --monitor "$@"
        ;;
    traffic)
        shift
        exec "$SCRIPT_DIR/lanimals_traffic" "$@"
        ;;
    netmap)
        shift
        exec "$SCRIPT_DIR/lanimals_netmap" "$@"
        ;;
    viznet)
        shift
        exec "$SCRIPT_DIR/lanimals_viznet" "$@"
        ;;
    fortress)
        shift
        exec "$SCRIPT_DIR/lanimals_fortress" "$@"
        ;;
    sysinfo)
        shift
        exec "$SCRIPT_DIR/lanimals_sysinfo" "$@"
        ;;
    hunter)
        shift
        exec "$SCRIPT_DIR/lanimals_hunter" "$@"
        ;;
    vulnscan)
        shift
        exec "$SCRIPT_DIR/lanimals_vulscan" "$@"
        ;;
    threats)
        STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
        if [ -f "$STATE_HOME/lanimals/logs/threats.log" ]; then
            echo -e "${BOLD}${MAROON}Recent Threats:${NORMAL}"
            tail -n 20 "$STATE_HOME/lanimals/logs/threats.log"
        else
            echo "No threats logged yet."
        fi
        ;;
    version|--version|-V)
        echo -e "${BOLD}${MAROON}LANimals${NORMAL} - Version ${VERSION}"
        echo "Local Network Intelligence Platform"
        ;;
    update)
        echo -e "${BOLD}${MAROON}[ UPDATE ]${NORMAL} Automatic update checking is not implemented."
        echo -e "Installed version: ${VERSION}"
        echo "Compare releases at https://github.com/GnomeMan4201/LANimals/releases"
        ;;
    help|--help|-h|"")
        show_help
        ;;
    *)
        echo "Unknown LANimals command: $1" >&2
        echo "Run 'lanimals help' for the command list and support status." >&2
        exit 2
        ;;
esac
