"""
Quick test: Can Luke grab Health Connect from Google Drive?
Run this on your Mac:  python3 ~/clawd/test_drive_grab.py
"""
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "whatsapp-agent"))

from gdrive_health import get_connector

print("Testing Google Drive Health Connect pipeline...\n")

gdrive = get_connector()

# Step 1: Auth
if not gdrive.available:
    print("❌ Google Drive API not available.")
    print("   Run: python3 ~/clawd/whatsapp-agent/setup_google_auth.py")
    sys.exit(1)
print("✅ Google Drive API authenticated\n")

# Step 2: Search
files = gdrive.search_health_connect_files(max_age_hours=48)
if not files:
    print("❌ No Health Connect files found in Drive (last 48 hours)")
    print("   Make sure your export is uploaded to Google Drive.")
    sys.exit(1)

print(f"✅ Found {len(files)} file(s):")
for f in files:
    size_mb = f['size'] / 1024 / 1024
    print(f"   📄 {f['name']} ({size_mb:.1f} MB) — {f['modifiedTime']}")

# Step 3: Download
latest = files[0]
local_path = gdrive.download_file(latest['id'], latest['name'])
if not local_path:
    print(f"\n❌ Download failed for {latest['name']}")
    sys.exit(1)
print(f"\n✅ Downloaded to: {local_path}")

# Step 4: Parse
from health_connect import HealthConnectParser
with HealthConnectParser(local_path) as parser:
    data = parser.extract_all()

print(f"\n✅ Parsed successfully:")
if 'weight' in data:
    print(f"   Weight: {data['weight']['lbs']} lbs ({data['weight']['timestamp']})")
if 'sleep' in data:
    print(f"   Sleep: {data['sleep']['total_hours']}h")
if 'exercise' in data:
    print(f"   Exercise: {len(data['exercise'])} sessions")
if 'steps' in data:
    print(f"   Steps: {data['steps']}")

print("\n🎉 Full pipeline working! The 8 AM daily grab will work.")
