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

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
KUBECONFIG="${KUBECONFIG:-${KUBECONFIG_PATH:-${HOME}/.kube/config}}"

check() {
    local label="$1"
    shift

    printf '%-36s ' "${label}"
    if "$@" >/tmp/homelab-recover-check.$$ 2>&1; then
        printf 'ok\n'
    else
        printf 'check\n'
        sed -n '1,3p' /tmp/homelab-recover-check.$$ | sed 's/^/    /'
    fi
    rm -f /tmp/homelab-recover-check.$$
}

has_gitea_backup() {
    find "${LAB_GITEA_BACKUP_DIR:-/home/jv/backups/gitea}" -maxdepth 1 -type f -name 'gitea-*.zip' -print -quit | grep -q .
}

has_state_backup() {
    find "${HOMELAB_TOFU_STATE_BACKUP_DIR:-${XDG_DATA_HOME:-${HOME}/.local/share}/homelab/tofu-state-backups}" -maxdepth 1 -type f -name 'tofu-state-*.tgz' -print -quit | grep -q .
}

main() {
    cat <<'EOF'
== Homelab Recovery Order ==

1. Restore Debian control host baseline.
   Command: ./jeannie validate
   Check: Docker, containerd, Tailscale, kubeconfig path, repo checkout.

2. Restore or verify Gitea.
   Commands:
     ./jeannie deploy-gitea
     ./jeannie drill-gitea-restore
     ./jeannie bootstrap-gitea-repo

3. Restore RPi DNS services.
   Commands:
     ./jeannie rpi-services
     ./jeannie doctor-rpi
     ./jeannie drill-pihole-restore

4. Verify Pimox template and worker storage.
   Commands:
     ./jeannie preflight
     ./jeannie doctor-preapply

5. Rebuild or start Kubernetes.
   If state exists and nodes are stopped: ./jeannie start-cluster
   If cluster must be rebuilt:          ./jeannie rebuild-cluster

6. Restore platform and apps from GitOps.
   Commands:
     ./jeannie apps
     ./jeannie gitops-status

7. Verify public edge.
   Commands:
     ./jeannie doctor-edge
     ./jeannie cert-check
     ./jeannie status

8. Capture final recovery evidence.
   Commands:
     ./jeannie release-snapshot
     ./jeannie backup-gitea
     ./jeannie state-backup

EOF

    echo "== Current Recovery Inputs =="
    check "repo validation" "${REPO_ROOT}/scripts/validate-homelab"
    check "Gitea backup archive" has_gitea_backup
    check "OpenTofu state backup" has_state_backup
    check "Pi-hole restore inputs" "${REPO_ROOT}/scripts/restore-drill" pihole
    check "Kubernetes API" kubectl --kubeconfig "${KUBECONFIG}" get --raw=/readyz

    cat <<'EOF'

== Recovery Rule ==
Prefer restoring source-of-truth first: repo, Gitea, DNS, state backups, then
rebuild compute. Treat Pimox worker disks as rebuildable unless a service has an
explicit backup and restore path.
EOF
}

main "$@"
