#!/bin/bash

echo "๐Ÿ—‘๏ธ Resetting Hyperliquid Trading Bot Data"
echo "========================================="

# Confirm with user
read -p "โš ๏ธ  This will delete ALL trading data, state, alarms, and logs. Are you sure? (yes/no): " confirm

if [ "$confirm" != "yes" ]; then
    echo "โŒ Reset cancelled."
    exit 1
fi

echo "๐Ÿงน Clearing data files..."

DATA_DIR="data"

# Create data directory if it doesn't exist (for idempotency, though removal is primary goal)
# mkdir -p $DATA_DIR

# Remove trading statistics
if [ -f "$DATA_DIR/trading_stats.sqlite" ]; then
    rm "$DATA_DIR/trading_stats.sqlite"
    echo "โœ… Removed $DATA_DIR/trading_stats.sqlite"
fi

# Remove legacy files (if they still exist)
rm -f "$DATA_DIR/trading_stats.json"
rm -f trading_stats.json.backup* # Old root backups
rm -f "$DATA_DIR/trading_stats.json.backup*"
echo "โœ… Removed legacy stats files (if any)"

# Remove market monitor state (legacy - now in SQLite)
rm -f "$DATA_DIR/market_monitor_state.json"
echo "โœ… Removed legacy market monitor state (if any)"

# Remove legacy bot state files (no longer used - all state now in SQLite)
rm -f "$DATA_DIR/trading_engine_state.json"
rm -f "bot_state.json"
echo "โœ… Removed legacy bot state files (if any)"

# Remove alarm data
if [ -f "$DATA_DIR/price_alarms.json" ]; then
    rm "$DATA_DIR/price_alarms.json"
    echo "โœ… Removed $DATA_DIR/price_alarms.json"
fi
rm -f alarms.json alarms.db # Legacy alarm files at root
echo "โœ… Removed legacy alarm data from root (if any)"

# Optionally, remove the entire data directory if it's now empty
# Check if directory exists and is empty
if [ -d "$DATA_DIR" ] && [ -z "$(ls -A $DATA_DIR)" ]; then
    rmdir "$DATA_DIR"
    echo "โœ… Removed empty $DATA_DIR directory"
elif [ -d "$DATA_DIR" ]; then
    echo "โ„น๏ธ  $DATA_DIR directory still contains other files."
fi

# Remove log files
rm -f *.log # Root logs (if any)
rm -rf logs/
echo "โœ… Removed log files and logs/ directory"

# Remove any temporary files
rm -f *.tmp *.temp
echo "โœ… Removed temporary files"

# Remove any Python cache
find . -type d -name "__pycache__" -exec rm -rf {} +
echo "โœ… Removed Python cache directories"

echo ""
echo "๐ŸŽ‰ Data reset complete!"
echo ""
echo "๐Ÿ“ What was cleared:"
echo "   โ€ข Persistent data from the '$DATA_DIR/' directory (stats, state, alarms)"
echo "   โ€ข Log files from 'logs/' directory and root"
echo "   โ€ข Legacy data files from the root directory (if found)"
echo "   โ€ข Trading statistics and P&L history"
echo "   โ€ข All completed trade records"
echo "   โ€ข Daily/weekly/monthly performance data"
echo "   โ€ข Bot state persistence (pending stop losses, order tracking)"
echo "   โ€ข Price alarms and notifications"
echo "   โ€ข Log files and debugging data"
echo "   โ€ข Enhanced position tracking data"

echo ""
echo "๐Ÿš€ Your bot is now ready for a fresh start!"
echo "   โ€ข Initial balance will be reset by the bot logic"
echo "   โ€ข All stats will start from zero"
echo "   โ€ข Position tracking will reinitialize"
echo "   โ€ข Bot state persistence will restart fresh"
echo "   โ€ข Pending stop losses will be cleared"

echo ""
echo "๐Ÿ’ก Next steps:"
echo "   1. Run your bot: python trading_bot.py (or your main script)"
echo "   2. Check /balance for initial balance status"
echo "   3. Start trading to build new statistics"