#!/usr/bin/env bash
# ============================================================
# HalaVoice — disaster recovery / restore
#
# Two recovery sources (code) + one database restore path:
#
#  A) From a local/downloaded full backup directory:
#       ops/restore.sh --from-backup /path/to/full-backup-<TS>
#     Restores code-config.tar.gz into the target, restores the
#     DB from agentlabs-db.sql.gz, reinstalls modules, rebuilds.
#
#  B) From GitHub (code only) + a DB dump you provide:
#       ops/restore.sh --from-github <git-url> [--db-dump db.sql.gz] [--env /path/.env]
#     Clones the repo, drops in your .env + DB dump, rebuilds.
#
#  C) Database only (into the existing checkout):
#       ops/restore.sh --db-only /path/to/agentlabs-db.sql.gz
#
# Flags: --target DIR (default: this repo root)   --yes (skip prompts)
#
# NOTE: the database restore is DESTRUCTIVE (drops & recreates objects
# in the target DB). You are prompted unless --yes is given.
# ============================================================
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"

MODE="" ; SRC="" ; DB_DUMP="" ; ENV_FILE="" ; TARGET="$REPO_ROOT" ; ASSUME_YES=0
while [ $# -gt 0 ]; do
  case "$1" in
    --from-backup) MODE=backup; SRC="$2"; shift ;;
    --from-github) MODE=github; SRC="$2"; shift ;;
    --db-only)     MODE=dbonly; DB_DUMP="$2"; shift ;;
    --db-dump)     DB_DUMP="$2"; shift ;;
    --env)         ENV_FILE="$2"; shift ;;
    --target)      TARGET="$2"; shift ;;
    --yes)         ASSUME_YES=1 ;;
    *) die "unknown arg: $1" ;;
  esac
  shift
done
[ -n "$MODE" ] || die "choose a mode: --from-backup | --from-github | --db-only (see header)"

confirm() {
  [ "$ASSUME_YES" = "1" ] && return 0
  read -r -p "$1 [type YES]: " a; [ "$a" = "YES" ] || die "aborted by user"
}

restore_db() {
  local dump="$1"
  [ -f "$dump" ] || die "DB dump not found: $dump"
  need psql
  local url; url="$(db_url "$TARGET/.env" 2>/dev/null || db_url)"
  warn "About to restore the database — this OVERWRITES current data."
  confirm "Restore DB from $(basename "$dump") into the target database?"
  log "Restoring database…"
  if [[ "$dump" == *.gz ]]; then gzip -dc "$dump" | psql "$url"; else psql "$url" < "$dump"; fi
  ok "Database restored"
}

case "$MODE" in
  backup)
    [ -d "$SRC" ] || die "backup dir not found: $SRC"
    log "Restoring CODE from $SRC/code-config.tar.gz → $TARGET"
    mkdir -p "$TARGET"
    confirm "Extract backup into $TARGET (existing files may be overwritten)?"
    tar -xzf "$SRC/code-config.tar.gz" -C "$TARGET"
    [ -f "$SRC/.env" ] && cp "$SRC/.env" "$TARGET/.env" && chmod 600 "$TARGET/.env" && ok ".env restored"
    "$OPS_DIR/setup-modules.sh"
    [ -f "$SRC/agentlabs-db.sql.gz" ] && restore_db "$SRC/agentlabs-db.sql.gz"
    ;;
  github)
    need git
    [ -n "$SRC" ] || die "--from-github needs a git URL"
    log "Cloning $SRC → $TARGET"
    [ -e "$TARGET/.git" ] && die "target already a git repo: $TARGET (choose an empty --target)"
    git clone "$SRC" "$TARGET"
    if [ -n "$ENV_FILE" ]; then cp "$ENV_FILE" "$TARGET/.env" && chmod 600 "$TARGET/.env" && ok ".env installed";
    else warn "no --env given: create $TARGET/.env from .env.example before starting"; fi
    ( cd "$TARGET" && "$TARGET/ops/setup-modules.sh" )
    [ -n "$DB_DUMP" ] && restore_db "$DB_DUMP"
    ;;
  dbonly)
    restore_db "$DB_DUMP"
    ;;
esac

ok "Restore finished."
echo "Next: cd $TARGET && pm2 start ecosystem.config.cjs   (or: pm2 restart agentlabs)"
