#!/usr/bin/env bash
# Wipe tenant data so /setup can run again (production cloud hub only). DESTRUCTIVE.
set -euo pipefail
[[ "$(id -u)" -eq 0 ]] || { echo "Run as root"; exit 1; }

DB="${1:-cnperpdimensions}"
APP_USER="${2:-cnperp}"

echo "WARNING: This deletes ALL companies, users, and tenant data in ${DB}."
read -r -p "Type YES to continue: " confirm
[[ "$confirm" == "YES" ]] || { echo "Aborted."; exit 1; }

sudo -u "$APP_USER" psql -d "$DB" -v ON_ERROR_STOP=1 <<'SQL'
TRUNCATE companies RESTART IDENTITY CASCADE;
SQL

systemctl restart cnperp
echo ""
echo "Done. Open https://erp.cnpsolutions.co.bw/setup for a fresh wizard."
