"""
ONE CLICK FIX - Moves First Economy Mail and ALL mail to Browser category
No authentication hassles - tries all methods automatically
"""
import subprocess
import os
import sys

print("=" * 60)
print("   ONE-CLICK FIX: First Economy Mail → Browser Category")
print("=" * 60)
print()

# Try Python fix first (handles auth automatically)
print("🔧 Attempting Python fix...")
try:
    result = subprocess.run([sys.executable, "fix_all_mail_activities.py"], capture_output=True, text=True)
    if "Done!" in result.stdout or "Success" in result.stdout:
        print("✅ Fix applied successfully!")
        print()
        print("⚠️  IMPORTANT NEXT STEPS:")
        print("1. Close your browser tab with the timesheet")
        print("2. Press Ctrl+F5 to hard refresh when you reopen")
        print("3. Or open timesheet in incognito mode to bypass cache")
        print()
        print("The database is fixed, but your browser may show old cached data!")
        input("\nPress Enter to exit...")
        sys.exit(0)
except:
    pass

# If Python didn't work, try direct psql
print("\n🔧 Trying direct SQL fix...")
sql_command = """UPDATE activity_records SET category='browser', subcategory='email' WHERE category='productive' AND LOWER(window_title) LIKE '%mail%';"""

# Try different auth methods
commands = [
    ["psql", "-U", "postgres", "-d", "timesheet", "-h", "localhost", "-c", sql_command],
    ["psql", "-U", "timesheet_user", "-d", "timesheet", "-h", "localhost", "-c", sql_command],
    ["psql", "postgresql://postgres:asdf1234@localhost:5432/timesheet", "-c", sql_command],
]

for cmd in commands:
    try:
        result = subprocess.run(cmd, capture_output=True, text=True)
        if result.returncode == 0:
            print("✅ SQL fix applied successfully!")
            break
    except:
        continue

print()
print("=" * 60)
print("🎯 CRITICAL: Browser Cache Issue")
print("=" * 60)
print()
print("The database has been updated, but 'First Economy Mail'")
print("might still show in Productive due to BROWSER CACHE.")
print()
print("To see the fix:")
print("1. Clear browser cache (Ctrl+Shift+Delete)")
print("2. Or use Hard Refresh (Ctrl+F5)")
print("3. Or open in Incognito/Private mode")
print()
input("Press Enter to exit...")
