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

YES=0
PURGE_I18N=0

for arg in "$@"; do
  case "$arg" in
    --yes) YES=1 ;;
    --purge-i18n) PURGE_I18N=1 ;;
  esac
done

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
DATA_DIR="$ROOT_DIR/data"
I18N_DIR="$ROOT_DIR/i18n"

echo "Full reset will remove runtime data, logs, and caches."
echo "Root: $ROOT_DIR"
echo "Data: $DATA_DIR"
if [ "$PURGE_I18N" -eq 1 ]; then
  echo "Preserve i18n: no"
else
  echo "Preserve i18n: yes"
fi

if [ "$YES" -ne 1 ]; then
  read -r -p "Type RESET to continue: " confirm
  if [ "$confirm" != "RESET" ]; then
    echo "Aborted."
    exit 1
  fi
fi

if [ -d "$DATA_DIR" ]; then
  rm -rf "$DATA_DIR"/*
fi

if [ "$PURGE_I18N" -eq 1 ] && [ -d "$I18N_DIR" ]; then
  rm -rf "$I18N_DIR"
fi

rm -rf "$ROOT_DIR/inbox" \
       "$ROOT_DIR/outbox" \
       "$ROOT_DIR/library" \
       "$ROOT_DIR/logs" \
       "$ROOT_DIR/quart_ui/state" \
       "$ROOT_DIR/__pycache__" || true

find "$ROOT_DIR" -maxdepth 1 -type f -name "*.log" -delete || true

echo "Reset complete. Restart the app to regenerate defaults."
