#!/usr/bin/env bash
# Full economy database reset - backs up bot.db, stops the bot, wipes every
# economy-related table (balances, catches, shop items/boosts, marriages,
# lottery, pets, bank ledger, quests, market listings, farm-category
# macro-detection history), then restarts the bot.
#
# Run from the bot's own directory: bash reset-economy-db.sh
set -euo pipefail

BOT_DIR="/var/www/html/bot"
DB_FILE="$BOT_DIR/data/bot.db"
BACKUP_DIR="$BOT_DIR/data/backups"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
BACKUP_FILE="$BACKUP_DIR/bot.db.pre-economy-reset.$TIMESTAMP.bak"

echo "This will PERMANENTLY wipe ALL economy data (balances, catches, shop"
echo "items, marriages, lottery, pets, bank history, quests) for every user."
echo "A backup will be made first at: $BACKUP_FILE"
read -p "Type RESET to continue: " CONFIRM
if [ "$CONFIRM" != "RESET" ]; then
  echo "Aborted - no changes made."
  exit 1
fi

echo "Stopping source service..."
systemctl stop source

mkdir -p "$BACKUP_DIR"
echo "Backing up $DB_FILE -> $BACKUP_FILE"
cp "$DB_FILE" "$BACKUP_FILE"

echo "Running economy reset..."
cd "$BOT_DIR"
node scripts/resetEconomyDb.js

echo "Restarting source service..."
systemctl start source

echo "Done. Backup saved at $BACKUP_FILE - keep it until you're sure everything looks right."
