#!/usr/bin/env bash

dump_config_snapshot() {
  local backup_dir="$1"
  local repo_root

  repo_root="$(backup_repo_root)"
  mkdir -p "$backup_dir/config"

  if [[ -f "$repo_root/.env" ]]; then
    backup_redact_env "$repo_root/.env" "$backup_dir/config/env.redacted"
  fi

  backup_safe_tar "$repo_root/config/.generated/ory" "$backup_dir/config/generated-ory"
  backup_safe_tar "$repo_root/gateway" "$backup_dir/config/gateway"

  mkdir -p "$backup_dir/config/compose"
  for compose_file in compose.infra.yaml compose.ory.yaml docker-compose.yaml; do
    if [[ -f "$repo_root/$compose_file" ]]; then
      cp "$repo_root/$compose_file" "$backup_dir/config/compose/$compose_file"
    fi
  done
}

restore_config_snapshot() {
  local backup_dir="$1"
  local repo_root
  local output_dir

  repo_root="$(backup_repo_root)"
  output_dir="$repo_root/config-restored"

  backup_require_path "$backup_dir/config"
  mkdir -p "$output_dir"
  cp -R "$backup_dir/config/." "$output_dir/"
  backup_log "Config snapshot was copied to $output_dir for manual review."
}
