
import sys
import os
from pathlib import Path
from datetime import datetime, timedelta

# Add project root to python path
sys.path.append(str(Path(__file__).parent.parent))

from health.services.data_sync import HealthDataSync
from health import config

def check_recent_files():
    print("\nChecking for files modified in the last 2 days...")
    data_dir = config.DATA_DIR
    recent_files = []
    
    cutoff_time = datetime.now().timestamp() - 2 * 24 * 3600
    
    for root, dirs, files in os.walk(data_dir):
        for file in files:
            file_path = Path(root) / file
            if file_path.stat().st_mtime > cutoff_time:
                recent_files.append((file_path, file_path.stat().st_mtime))
    
    if not recent_files:
        print("No files modified in the last 2 days found.")
    else:
        print(f"Found {len(recent_files)} files modified in the last 2 days:")
        # Sort by mtime desc
        recent_files.sort(key=lambda x: x[1], reverse=True)
        for path, mtime in recent_files[:10]:
            print(f"  {datetime.fromtimestamp(mtime)} - {path.relative_to(data_dir)}")
        if len(recent_files) > 10:
            print(f"  ... and {len(recent_files) - 10} more.")

def check_sync_status():
    print("\nChecking Sync Status via HealthDataSync...")
    try:
        sync = HealthDataSync()
        status = sync.get_sync_status()
        
        print(f"{'Metric':<20} | {'Status':<15} | {'Last Sync Date':<15} | {'Records':<10}")
        print("-" * 70)
        
        for metric, info in status.items():
            last_sync = info.get('last_sync_date')
            last_sync_str = str(last_sync) if last_sync else "N/A"
            print(f"{metric:<20} | {info.get('status', 'unknown'):<15} | {last_sync_str:<15} | {info.get('total_records', 0):<10}")
            
    except Exception as e:
        print(f"Error checking sync status: {e}")

if __name__ == "__main__":
    check_recent_files()
    check_sync_status()
