#!/bin/bash

# TimeSafari Database Clearing Script
# Author: Matthew Raymer
# Date: 2025-07-11

set -e

echo "=== TimeSafari Database Clearing Script ==="
echo ""

# Detect platform
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    PLATFORM="linux"
elif [[ "$OSTYPE" == "darwin"* ]]; then
    PLATFORM="macos"
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
    PLATFORM="windows"
else
    PLATFORM="unknown"
fi

echo "Detected platform: $PLATFORM"
echo ""

# Function to clear Electron database
clear_electron_database() {
    echo "🧹 Clearing Electron Database..."
    
    local db_path=""
    case $PLATFORM in
        "linux")
            db_path="$HOME/Databases/TimeSafari"
            ;;
        "macos")
            db_path="$HOME/Library/Application Support/TimeSafari"
            ;;
        "windows")
            db_path="$APPDATA/TimeSafari"
            ;;
        *)
            echo "❌ Unknown platform: $PLATFORM"
            exit 1
            ;;
    esac
    
    if [[ -d "$db_path" ]]; then
        echo "Found database at: $db_path"
        echo "Removing database files..."
        rm -rf "$db_path"/*
        echo "✅ Electron database cleared successfully!"
    else
        echo "ℹ️  No database found at: $db_path"
        echo "   (This is normal if you haven't run the app yet)"
    fi
}

# Function to show web browser instructions
show_web_instructions() {
    echo "🌐 Web Browser Database Clearing Instructions:"
    echo ""
    echo "For reliable database clearing in web browsers, use a custom data directory:"
    echo ""
    
    case $PLATFORM in
        "linux")
            echo "📁 Create custom data directory:"
            echo "   mkdir -p ~/timesafari-dev-data"
            echo ""
            echo "🚀 Start browser with custom data directory:"
            echo "   # Chrome/Chromium:"
            echo "   google-chrome --user-data-dir=~/timesafari-dev-data"
            echo "   # Firefox:"
            echo "   firefox --profile ~/timesafari-dev-data"
            echo ""
            echo "🧹 Clear database:"
            echo "   rm -rf ~/timesafari-dev-data"
            ;;
        "macos")
            echo "📁 Create custom data directory:"
            echo "   mkdir -p ~/timesafari-dev-data"
            echo ""
            echo "🚀 Start browser with custom data directory:"
            echo "   # Chrome:"
            echo "   /Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --user-data-dir=~/timesafari-dev-data"
            echo "   # Firefox:"
            echo "   /Applications/Firefox.app/Contents/MacOS/firefox --profile ~/timesafari-dev-data"
            echo ""
            echo "🧹 Clear database:"
            echo "   rm -rf ~/timesafari-dev-data"
            ;;
        "windows")
            echo "📁 Create custom data directory:"
            echo "   mkdir %USERPROFILE%\\timesafari-dev-data"
            echo ""
            echo "🚀 Start browser with custom data directory:"
            echo "   # Chrome:"
            echo "   \"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\" --user-data-dir=%USERPROFILE%\\timesafari-dev-data"
            echo "   # Firefox:"
            echo "   \"C:\\Program Files\\Mozilla Firefox\\firefox.exe\" --profile %USERPROFILE%\\timesafari-dev-data"
            echo ""
            echo "🧹 Clear database:"
            echo "   rmdir /s /q %USERPROFILE%\\timesafari-dev-data"
            ;;
    esac
}

# Function to show manual browser clearing
show_manual_browser_clearing() {
    echo "🔧 Manual Browser Database Clearing:"
    echo ""
    echo "If you prefer to use your regular browser profile:"
    echo ""
    echo "1. Open browser Developer Tools (F12)"
    echo "2. Go to Application/Storage tab"
    echo "3. Find 'IndexedDB' section"
    echo "4. Delete 'TimeSafari' database"
    echo "5. Refresh the page"
    echo ""
    echo "⚠️  Note: This only clears the current site's data."
    echo "   For complete clearing, use the custom data directory method above."
}

# Main script logic
echo "Choose your platform:"
echo "1. Electron (Desktop App)"
echo "2. Web Browser - Custom Data Directory"
echo "3. Web Browser - Manual Clearing"
echo "4. Show all options"
echo ""

read -p "Enter your choice (1-4): " choice

case $choice in
    1)
        clear_electron_database
        ;;
    2)
        show_web_instructions
        ;;
    3)
        show_manual_browser_clearing
        ;;
    4)
        echo "=== All Database Clearing Options ==="
        echo ""
        clear_electron_database
        echo ""
        show_web_instructions
        echo ""
        show_manual_browser_clearing
        ;;
    *)
        echo "❌ Invalid choice. Please run the script again."
        exit 1
        ;;
esac

echo ""
echo "✅ Database clearing instructions completed!"
echo ""
echo "💡 Tip: After clearing the database, restart your development server:"
echo "   npm run build:electron:dev    # For Electron"
echo "   npm run build:web:dev         # For Web" 