#!/usr/bin/env bash
# Nexus — 部署运行时检测（Docker 1Panel / Supervisor 裸机）
#
#   source deploy/detect_deploy_runtime.sh
#   ROOT="$(resolve_nexus_root)"
#   RUNTIME="$(detect_nexus_runtime "$ROOT")"   # docker | supervisor | unknown
set -euo pipefail

_COMPOSE_FILE="docker/docker-compose.prod.yml"
_COMPOSE_1PANEL="docker/docker-compose.1panel.yml"
_ENV_PROD="docker/.env.prod"

resolve_nexus_root() {
    local hint="${1:-}"
    if [[ -n "$hint" && -d "$hint/server" ]]; then
        echo "$hint"
        return 0
    fi
    if [[ -n "${NEXUS_ROOT:-}" && -d "${NEXUS_ROOT}/server" ]]; then
        echo "$NEXUS_ROOT"
        return 0
    fi
    if [[ -n "${NEXUS_DEPLOY_PATH:-}" && -d "${NEXUS_DEPLOY_PATH}/server" ]]; then
        echo "$NEXUS_DEPLOY_PATH"
        return 0
    fi
    local d
    for d in /opt/nexus /www/wwwroot/api.synaglobal.vip; do
        if [[ -d "$d/server" ]]; then
            echo "$d"
            return 0
        fi
    done
    for d in /www/wwwroot/*/; do
        [[ -d "${d}server" ]] || continue
        echo "${d%/}"
        return 0
    done
    if command -v supervisorctl >/dev/null 2>&1; then
        local cwd
        cwd="$(supervisorctl status nexus 2>/dev/null | grep -oP 'cwd=\K[^ ,]+' || true)"
        if [[ -n "$cwd" && -d "$cwd/server" ]]; then
            echo "$cwd"
            return 0
        fi
    fi
    return 1
}

_has_1panel_network() {
    docker network inspect 1panel-network >/dev/null 2>&1
}

detect_nexus_runtime() {
    local root="$1"
    if [[ -f "$root/$_ENV_PROD" ]] && command -v docker >/dev/null 2>&1 \
        && docker compose version >/dev/null 2>&1; then
        local -a args=(-f "$root/$_COMPOSE_FILE")
        if _has_1panel_network && [[ -f "$root/$_COMPOSE_1PANEL" ]]; then
            args+=(-f "$root/$_COMPOSE_1PANEL")
        fi
        local prof q
        prof="$root/docker/.host-profile"
        [[ -f "$prof" ]] || prof="$root/docker/profiles/2c8g.env"
        if [[ -f "$prof" ]]; then
            q="$(cd "$root" && docker compose "${args[@]}" \
                --env-file "$root/$_ENV_PROD" --env-file "$prof" \
                ps -q nexus 2>/dev/null || true)"
            if [[ -n "$q" ]]; then
                echo docker
                return 0
            fi
        fi
    fi
    if command -v supervisorctl >/dev/null 2>&1 \
        && supervisorctl status nexus >/dev/null 2>&1; then
        echo supervisor
        return 0
    fi
    echo unknown
}

nexus_publish_port_for() {
    local root="$1"
    local port
    port="$(grep -E '^NEXUS_PUBLISH_PORT=' "$root/$_ENV_PROD" 2>/dev/null | head -1 | cut -d= -f2- | tr -d '\r" ' || true)"
    if [[ -z "$port" && -f "$root/.env" ]]; then
        port="$(grep -E '^NEXUS_PORT=' "$root/.env" 2>/dev/null | head -1 | cut -d= -f2- | tr -d '\r" ' || true)"
    fi
    echo "${port:-8600}"
}

nexus_restart_service() {
    local root="$1"
    local runtime
    runtime="$(detect_nexus_runtime "$root")"
    case "$runtime" in
        docker)
            local cname
            cname="$(docker ps --format '{{.Names}}' 2>/dev/null | grep -E 'nexus-prod-nexus|nexus-nexus-1' | head -1 || true)"
            if [[ -n "$cname" ]]; then
                docker restart "$cname"
                return 0
            fi
            return 1
            ;;
        supervisor)
            supervisorctl restart nexus
            ;;
        *)
            return 1
            ;;
    esac
}

nexus_has_ops_cron() {
    command -v crontab >/dev/null 2>&1 \
        && crontab -l 2>/dev/null | grep -q 'nexus-health-monitor'
}

suggest_ops_cron() {
    local root="$1"
    if nexus_has_ops_cron; then
        return 0
    fi
    echo ""
    echo "[WARN] 未检测到 health_monitor / db_backup cron"
    echo "       建议: sudo bash ${root}/deploy/install_ops_cron.sh"
    echo "       或:   sudo nx cron"
}

prompt_ops_cron_if_missing() {
    local root="$1"
    if nexus_has_ops_cron; then
        echo "[INFO] Crontab 已含 health_monitor / db_backup"
        return 0
    fi
    suggest_ops_cron "$root"
    if [[ ! -t 0 ]]; then
        return 0
    fi
    local ans
    read -r -p "现在安装巡检/备份 cron? [Y/n]: " ans
    if [[ "$ans" == "n" || "$ans" == "N" ]]; then
        echo "[WARN] 已跳过 cron 安装"
        return 0
    fi
    bash "${root}/deploy/install_ops_cron.sh"
}
