#!/bin/bash
# =============================================================================
# admin-panel.sh -- Panel Administration Dispatcher
# =============================================================================
# Category: ce | Bird: kestrel | Tier: 2 | Node-aware: yes
# Dispatches to: panel/admin-panel-files.sh, panel/admin-panel-config.sh,
#                panel/admin-panel-db.sh, panel/admin-panel-email.sh
# Actions: --files, --config, --db, --email, --status, --send, --digest, --flush
# =============================================================================

TOOL_NAME="admin-panel"
TOOL_VERSION="3.0.0"
TOOL_TIER=2
TOOL_BIRD="kestrel"
TOOL_LOG="/opt/binary/logs/tools/${TOOL_NAME}.log"

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
if [[ -f "${SCRIPT_DIR}/common.sh" ]]; then source "${SCRIPT_DIR}/common.sh"
elif [[ -f "${SCRIPT_DIR}/../common.sh" ]]; then source "${SCRIPT_DIR}/../common.sh"
else echo "FATAL: common.sh not found" >&2; exit 1; fi
export BIRD_ID="${TOOL_BIRD}"

PANEL_DIR="${SCRIPT_DIR}/panel"

_dispatch_panel() {
    local module="$1"; shift
    local script="${PANEL_DIR}/admin-panel-${module}.sh"
    if [ -f "$script" ]; then
        bash "$script" "$@"
    else
        log_error "Panel module not found: ${module} (expected ${script})"
        return 1
    fi
}

_do_send_impl() {
    echo "=== Panel Status: $(ts_local) ==="
    echo "  Node: ${NODE_ID}"
    echo "  Modules:"
    for m in files config db email; do
        local s="${PANEL_DIR}/admin-panel-${m}.sh"
        if [ -f "$s" ]; then echo "  [OK] ${m}"
        else echo "  [MISSING] ${m}"; fi
    done
    tool_audit "send" "node=${NODE_ID}"
}

_do_status_impl() {
    local modules=0 missing=0
    for m in files config db email; do
        [ -f "${PANEL_DIR}/admin-panel-${m}.sh" ] && modules=$((modules+1)) || missing=$((missing+1))
    done
    local s="healthy"; [ "$missing" -gt 0 ] && s="degraded"
    echo "{\"tool\":\"${TOOL_NAME}\",\"node\":\"${NODE_ID}\",\"status\":\"${s}\",\"modules\":${modules},\"missing\":${missing},\"checked_at\":\"$(date -u +%Y-%m-%dT%H:%M:%SZ)\"}"
}

_do_digest_impl() { echo "=== Panel Report: ${NODE_ID} ==="; _do_send_impl; }
_do_flush_impl() { [ -f "$TOOL_LOG" ] && truncate -s 0 "$TOOL_LOG"; echo "Flush complete"; }

tool_help() {
    echo "${TOOL_NAME} v${TOOL_VERSION} -- Panel Administration"
    echo "  --files [args]   File management"
    echo "  --config [args]  Configuration management"
    echo "  --db [args]      Database management"
    echo "  --email [args]   Email management"
    echo "  --send|--status|--digest|--flush"
}

do_send()   { tool_wrap "send"   _do_send_impl "$@"; }
do_status() { tool_wrap "status" _do_status_impl "$@"; }

tool_init; trap 'tool_complete' EXIT
case "${1:---help}" in
    --send) shift; do_send "$@" ;; --status) shift; do_status "$@" ;;
    --digest) _do_digest_impl ;; --flush) _do_flush_impl ;;
    --files) shift; _dispatch_panel "files" "$@" ;;
    --config) shift; _dispatch_panel "config" "$@" ;;
    --db) shift; _dispatch_panel "db" "$@" ;;
    --email) shift; _dispatch_panel "email" "$@" ;;
    --help) tool_help ;; *) handle_standard_command "$@" ;;
esac
