import argparse
import shutil
import sys
from pathlib import Path


def _confirm() -> None:
    value = input("Type RESET to continue: ").strip()
    if value != "RESET":
        print("Aborted.")
        sys.exit(1)


def _remove_path(path: Path) -> None:
    if not path.exists():
        return
    if path.is_file() or path.is_symlink():
        try:
            path.unlink()
        except Exception:
            pass
        return
    try:
        shutil.rmtree(path, ignore_errors=True)
    except Exception:
        pass


def main() -> int:
    parser = argparse.ArgumentParser(
        description="Full reset for Gnomenall runtime data"
    )
    parser.add_argument("--yes", action="store_true", help="Skip confirmation prompt")
    parser.add_argument("--purge-i18n", action="store_true", help="Delete i18n too")
    args = parser.parse_args()

    root_dir = Path(__file__).resolve().parent.parent
    data_dir = root_dir / "data"
    i18n_dir = root_dir / "i18n"

    print("Full reset will remove runtime data, logs, and caches.")
    print(f"Root: {root_dir}")
    print(f"Data: {data_dir}")
    print(f"Preserve i18n: {'no' if args.purge_i18n else 'yes'}")

    if not args.yes:
        _confirm()

    if data_dir.exists():
        for child in data_dir.iterdir():
            _remove_path(child)

    if args.purge_i18n:
        _remove_path(i18n_dir)

    targets = [
        root_dir / "inbox",
        root_dir / "outbox",
        root_dir / "library",
        root_dir / "logs",
        root_dir / "quart_ui" / "state",
        root_dir / "__pycache__",
    ]
    for target in targets:
        _remove_path(target)

    for log_file in root_dir.glob("*.log"):
        _remove_path(log_file)

    print("Reset complete. Restart the app to regenerate defaults.")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
