#!/usr/bin/env bash
set -euo pipefail

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

run_check() {
    local name="$1"
    shift
    echo "## ${name}"
    set +e
    "$@" 2>&1
    local status=$?
    set -e
    echo "exit_status=${status}"
}

tmp_file="$(mktemp "${TMPDIR:-/tmp}/jeannie-capacity-advisor.XXXXXX")"
trap 'rm -f "$tmp_file"' EXIT

{
    run_check "capacity" "${REPO_ROOT}/scripts/capacity-report" --only problems
    run_check "resource-budget" "${REPO_ROOT}/scripts/resource-budget"
    run_check "control-plane" "${REPO_ROOT}/scripts/control-plane" status
} >"$tmp_file"

python3 - "$tmp_file" <<'PY'
import re
import sys

text = open(sys.argv[1], encoding="utf-8").read()
lowered = text.lower()

missing_match = re.search(r"(\d+)\s+pods missing requests/limits", text)
missing_count = int(missing_match.group(1)) if missing_match else 0

pressure = any(token in lowered for token in ["diskpressure", "memorypressure", "node pressure"])
worker_storage_warn = "worker storage" in lowered and "warn" in lowered
non_system_control_plane = False
control_plane_section = text.split("## control-plane", 1)[-1]
for line in control_plane_section.splitlines():
    if re.match(r"\s*(argocd|website-production|demos-static|heimdall|arr-stack|security-lab|monitoring)\s+", line):
        non_system_control_plane = True
        break

print("Jeannie Capacity Advisor")
print("========================")

recommendations = []
if missing_count:
    recommendations.append(
        (
            "Fix resource requests/limits first",
            f"{missing_count} pods are missing requests/limits, so scheduling and capacity math are not trustworthy yet.",
            "./jeannie resource-budget --details",
            "code/config: add resources to app manifests or Helm values",
        )
    )
if non_system_control_plane:
    recommendations.append(
        (
            "Keep app/data-plane pods off Debian",
            "Non-system workloads are still scheduled on the Debian control-plane host.",
            "./jeannie control-plane taint",
            "scheduling: taint Debian after Pimox workers are Ready",
        )
    )
if pressure or worker_storage_warn:
    recommendations.append(
        (
            "Consider adding or recreating Pimox worker capacity",
            "Cluster pressure or worker storage pressure is visible after current placement checks.",
            "./jeannie workers list",
            "capacity: increase LAB_PIMOX_WORKER_COUNT or recreate the pressured worker",
        )
    )

if not recommendations:
    recommendations.append(
        (
            "No new Pimox VM needed from current signals",
            "No capacity warning, node pressure, or control-plane placement issue was detected in the sampled reports.",
            "./jeannie capacity",
            "watch: rerun after adding workloads",
        )
    )

for index, (title, why, command, fix) in enumerate(recommendations, start=1):
    print(f"\n{index}. {title}")
    print(f"   Why: {why}")
    print(f"   Next command: {command}")
    print(f"   Fix path: {fix}")

print("\nRule of thumb")
print("- Add requests before deciding you need more VMs.")
print("- Taint Debian once Pimox workers are stable.")
print("- Add a Pimox VM only when real pressure remains after placement and requests are cleaned up.")
PY
